6

#PSTip Dynamically hiding a function from the debugger in PowerShell ISE

Note: This tip requires PowerShell 4.0 or above.

In this great post, Tobias walked you through the process of hiding functions from the debugger. In this tip, I want to show a dynamic way of doing the same, a way that doesn’t require access to the function code if for any reason you cannot decorate a function with the mentioned attribute. Here goes…

Starting in Windows 4.0 script blocks have a new property: DebuggerHidden.

PS> ${function:test}|gm d*

   TypeName: System.Management.Automation.ScriptBlock

Name           MemberType Definition
----           ---------- ----------
DebuggerHidden Property   bool DebuggerHidden {get;set;}

You can get its value to determine if a function is hidden from the debugger but more importantly, you can set its value and instruct the debugger to skip the function. Let’s create a function and try to debug it. Put the following code in a script file and save it (debugging works only for saved scripts).

Now move your cursor to the function call (line 6) and press F9 to place a new breakpoint. Press F5 to execute the script and then start pressing F11 to step into the code, you’ll see that you’re the debugger steps into the function body and it is executed line by line until eventually you get the return value of the function.

Now let’s set ask debugger to ignore and “step over” the function:

Repeat the debugging process described earlier. Notice that now, when you press the  F11 key, the debugger “ignores” the function and you immediately get the result.

Filed in: Columns, Tips and Tricks Tags: , , , ,

6 Responses to "#PSTip Dynamically hiding a function from the debugger in PowerShell ISE"

  1. alzdba says:

    apparently this doesn’t work for functions that follow the verb-noun guidelines 🙁

    $function:write-ScriptStep.DebuggerHidden = $true

    results to “Unexpected token ‘-ScriptStep’ in expression or statement.”

    How can I use this with functions that follow the guideline ?

Leave a Reply

Submit Comment

© 2016 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: