Conditional Breakpoints

by Jan 8, 2015

PowerShell 3.0 and later

The PowerShell ISE supports line breakpoints only: they make the code stop once the debugger hits that particular line. You can toggle line breakpoints in the PowerShell ISE by pressing F9. Just make sure the script is saved. Breakpoints do not work in unsaved scripts ("Untitled").

A more sophisticated approach uses dynamic (or "conditional") breakpoints. These are not bound to a particular line, but rather to a particular situation.

To make a script stop whenever a new value is assigned to a variable, try this sample script (make sure you save it before you run it):

$bp = Set-PSBreakpoint -Variable a -Mode Write -Script $psise.CurrentFile.FullPath

$a = 1
$a

$a

$a = 200
$a

Remove-PSBreakpoint -Breakpoint $bp

When you run it, the PowerShell debugger will automatically stop the script whenever a new value is assigned to the variable $a.

You can even bind sophisticated conditions to it. This example will break only when an integer value greater than 100 is assigned to $a:

$Condition = { if ($a -is [Int] -and $a -gt 100) { break }  }
$bp = Set-PSBreakpoint -Variable a -Mode Write -Script $psise.CurrentFile.FullPath -Action $Condition

$a = 1
$a

$a

$a = 200
$a

Remove-PSBreakpoint -Breakpoint $bp

Twitter This Tip! ReTweet this Tip!