Hiding Code from ISE Debugger

by Aug 8, 2013

Typically, when you set breakpoints in your script and then step through your code using F11 or "Debug/Step Into", the debugger will visit all lines that are executed.

Sometimes, however, you may not want to step through some functions, or you want to make sure that the code in a script module won't be stepped by the debugger.

While you can press F10 or use "Debug/Step Over" manually to do so, there is also a secret PowerShell attribute you can use to declare code as "hidden". Functions declared with this attribute still execute as usual. When debugging your code, however, the function will always execute as a whole, and the debugger will not step through the code.

Here is an example:

function test {
    param($a)

    Get-Service


}

"Hello"
test
"Hurray"

When you save this code as a script and then put a breakpoint on "Hello" (for example, by pressing F9 in the ISE editor), once you run the code, the debugger will step through the entire code line by line once you press F11.

By adding the secret attribute [System.Diagnostics.DebuggerHidden()], the function becomes invisible to the debugger and now behaves like a cmdlet (or external command): The function test executes, but the debugger no longer steps through the function code:

function test {
    [System.Diagnostics.DebuggerHidden()]
    param($a)

    Get-Service


}

"Hello"
test
"Hurray" 

Twitter This Tip! ReTweet this Tip!