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