Problem
You want to access detailed debugging or diagnostic information for the execution of a script or command.
Solution
To trace a script as it executes, use the Trace parameter of the SetPsDebug cmdlet.
To view detailed trace output for the PowerShell engine and its cmdlets, use the TraceCommand cmdlet.
Discussion
The SetPsDebug cmdlet lets you configure the amount of debugging detail that PowerShell provides during the execution of a script. By setting the Trace parameter, PowerShell lets you see the lines of script as PowerShell executes them.
When you want to investigate issues in the way that your code interacts with PowerShell, or with PowerShell commands, use the TraceCommand cmdlet. The TraceCommand cmdlet provides a huge amount of detail, intended mainly for indepth problem analysis.
For example, to gain some insight into why PowerShell can’t seem to find your script:
TraceCommand CommandDiscovery PsHost { ScriptInTheCurrentDirectory.ps1 }
The TraceCommand cmdlet takes a trace source (for example, CommandDiscovery), a destination (usually –PsHost or –File), and a script block to trace. The output of this command shows that PowerShell never actually searches the current directory for your script, so you need to be explicit: .\ScriptInTheCurrentDirectory.ps1.
For more information about the TraceCommand cmdlet, type GetHelp TraceCommand. To learn what trace sources are available, see the command GetTraceSource.