More PowerShell Trace Window Fun

On my last Friday Fun, I posted an article about using Internet Explorer as a trace window. The idea was to put debug or trace messages in a separate application. I received a comment on the post that suggested I could do a similar thing using the Debug View utility from Sysinternals. This application is used to capture debug messages so I thought I’d give it a try.

After you download it, you will run to manually run it once to accept licensing terms and setup a filter. I’m assuming you don’t regularly use this program for anything else. Depending on your computer you may not need a filter but it is the best way to work with my Debug-Message function.

The essence of this function is the [System.Diagnostics.Debug]::WriteLine($Message,$Category) line. The Category will show up as a prefix to the message in the Debug View window. I set a filter in Debug View on that category, ie PS Trace*. This function, like my IE trace function, relies on a variable $TraceEnabled to be set to $True. If the dbgview.exe process isn’t running, the function starts it. I have hardcoded the path to dbgview.exe which you’ll need to adjust. The easiest approach is to drop dbgview.exe into your Windows folder or modify your %PATH% variable to include your Sysinternals folder.

Using the function is no different than my IE version. In fact, in my demo script all I needed to do was change which script gets dot-sourced.

I added a “Trace” alias to my new Debug View function so don’t have to change anything else. When I run my script using the -Trace parameter, I get a handy trace window like this.

dbgview-trace

What’s handy about this utility is that it is easy to save the results to a file. I also don’t have to deal with messy COM objects. If you don’t setup the filter ahead of time you may have a hard time finding the trace messages from your script. But that’s your choice.

What do you think?