PowerShell:
Using a debugger

How to:

In PowerShell, you can debug scripts using the built-in PowerShell Integrated Scripting Environment (ISE) or Visual Studio Code (VS Code) with the PowerShell extension. Here’s how to use breakpoints in both:

PowerShell ISE:

# Set a breakpoint on a specific line
Set-PSBreakpoint -Script .\MyScript.ps1 -Line 5

# Run your script normally
.\MyScript.ps1

# When the script hits the breakpoint, you can inspect variables
$myVariable

# Continue execution
Continue

Visual Studio Code:

# Open your PowerShell script in VS Code.
# Click to the left of the line number to set a breakpoint.
# Start debugging by pressing F5 or clicking 'Start Debugging'.

# VS Code will stop execution at your breakpoint.
# Use the debug panel to watch variables, inspect call stack, and control the flow.

Debugging in both environments lets you step in (F11), step over (F10), and step out (Shift+F11) while debugging.

Deep Dive

Historically, debugging in PowerShell was a tad clunky; it required a lot of Write-Host lines to output variable states or the classic trial-and-error method. With the advent of PowerShell ISE, and more recently, VS Code with its rich debugging features, PowerShell debugging became nearly as intuitive as in full-fledged programming languages.

Alternatives to PowerShell’s native debugging tools include third-party tools like PowerGUI or using robust IDEs like Visual Studio with a PowerShell plugin.

When implementing a debugger, consider the script scope, especially when working with dot-sourced scripts or modules. Breakpoints can be condition-based, variable change-based, or line-based, allowing for precise control during a debugging session.

Moreover, with the transition to PowerShell Core (cross-platform PowerShell), debugging has largely moved into the hands of VS Code, which provides a consistent experience across different platforms.

See Also

For more on debugging in PowerShell: