V2 Quick Tips: Finding Extra Outputs From Your Script

PowerShell Team

One of the nifty features of functions in PowerShell is that they can return as many results from as many places as you’d like.  In fact, any method or cmdlet that would give you an output if you try it on the command line will also return values from a function.  While this can give you a lot of powerful capabilities (like easily building functions that return a list of items), it can also be a source of pain, especially if you have a decently sized script that, every once in a while, produces an unexpected output mixed with expected output.  Since you cannot restrict what types get outputted from a function,and since commands in PowerShell almost always end up in a pipeline with other commands, these subtle unexpected outputs can cause big problems later on.

Recently, a support engineer asked our discussion list how to track down these extra outputs.  I pointed him to a nifty trick with the V2 debugger.

You see, every command that outputs will call the command Out-Default.  The Integrated Scripting Environment may make it easy to set line breakpoints (its F9), but the debugger in PowerShell V2 can also break on any command, including commands that aren’t explicitly in your script.  By setting a breakpoint on Out-Default and simply running through my script, I can actually find all of the points it outputs.

I do this with the following line:

$null = Set-PSBreakpoint -Command Out-Default

Notice the $null = ?. That’s there to suppress the natural output of Set-PSBreakpoint, which is a breakpoint object.  If I don’t put this there, then Set-PSBreakpoint would output, which would call Out-Default, which would break into the debugger.  Whenever I find the line that I don’t want to output, I simply put $null = at the beginning of that line.

After I’ve set this breakpoint, I can just run my script.  As I run it, whenever it outputs it will break on Out-Default, and I can simply step over to the next line (F10).  This will highlight the line directly after the line that outputted.  If you didn’t expect one of these lines to generate output, simply put $null = at the beginning of that line.

Hope this Helps,

James Brundage [MSFT]


Discussion is closed.

Feedback usabilla icon