PS variable/method to detect if we are currently debugging or not

Hi,

I would like to have the detection ability for a script if it’s launched normally or via vscode debbuging.

Possible?

You can check to see if you are running in the standard ConsoleHost or the VS Code Host.

PS C:\Users\adamr\Desktop> $Host


Name             : Visual Studio Code Host
Version          : 2021.4.1
InstanceId       : 525220a3-710a-4906-8296-c92dcb8cd84e
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.EditorServices.Services.PowerShellContext.EditorServicesPSHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace
PS C:\Users\adamr> $Host

Name             : ConsoleHost
Version          : 7.1.0
InstanceId       : e2e3f628-12e6-4653-baf9-bb5c217e437d
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

You can also check to see if the debugger is sitting on a breakpoint by checking the runspace.

$Host.Runspace.Debugger.IsActive
1 Like