4

#PSTip How can I determine if I’m in debugging mode?

Note: This tip requires PowerShell 2.0 or above.

When a session is being debugged, PowerShell populates an automatic variable called $PSDebugContext which contains an object that has Breakpoints and InvocationInfo properties information about the debugging environment. Visually you can see this by looking at the prompt. In debugging mode, “[DBG]” is added to the prompt:

[DBG] PS C:\>

If you need to detect this programatically, you can use the Test-Path cmdlet and check for the existence of the variable . If $PSDebugContext exists (not $null),  you are in debugging mode.

PS> Test-Path Variable:PSDebugContext
Filed in: Columns, Tips and Tricks Tags: , , ,

4 Responses to "#PSTip How can I determine if I’m in debugging mode?"

  1. texaganian says:

    How would you determine if a script has been run with the -Debug parameter? Doing that doesn’t seem to set $PSDebugContext.

  2. Shay Levy says:

    Check the $DebugPreference variable.
    If the script was called with the Debug switch the value $DebugPreference changes to ‘Inquire’.

Leave a Reply

Submit Comment

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