PowerShell 2.0: One Cmdlet at a Time 104 Trace-Command

Continuing the series looking at new cmdlets available in PowerShell 2.0. This time we look at the Trace-Command cmdlet.

What can I do with it?

Begin a trace of a command or expression.

Example:

Examine debug info for Parameter Binding when piping a string through to Get-Service.

Trace-Command -Name ParameterBinding -Option All -Expression {‘winmgmt’ | Get-Service} -PSHost

You will see it is possible to work through the debug info to find out what is happening:

Note: it is also possible to output the debug info to a file, simply remove the PSHost parameter and use FilePath instead.

Trace-Command -Name ParameterBinding -Option All -Expression {‘winmgmt’ | Get-Service} -FilePath C:\Debug.txt

The resulting debug info is now easily viewable in Notepad.

How could I have done this in PowerShell 1.0?

You could have used Set-TraceSource , but Trace-Command applies the trace only to the specified command.

1000 things 1% better!