#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

About the author: Shay Levy

Shay Levy is a Co-founder and editor of the PowerShell Magazine. He is a multiple-year recipient of the Microsoft MVP award, and a Microsoft Certified Trainer (MCT). Shay often covers PowerShell related topics on his blog and you can also follow him on Twitter at @ShayLevy

Related Posts