Creating Dynamic Breakpoints

by Jan 1, 2013

The PowerShell 3.0 ISE editor has a simple debugger built-in that supports line breakpoints. Simply press F9 to set a breakpoint (which only works if the file has been saved).

In addition, Set-PSBreakpoint can set dynamic breakpoints. This one would halt whenever ForEach-Object is executed:

PS> Set-PSBreakpoint -Command ForEach-Object

And this one would halt for each object that has the noun "Object", but only if the command is called from the script that is in the current ISE tab:

PS> Set-PSBreakpoint -Command *-Object -Script ($psise.CurrentFile.FullPath)

Even more advanced debugging is doable. This line halts whenever the variable $cpu gets assigned a $null value:

PS> Set-PSBreakpoint -Variable cpu -Mode Write -Script ($psise.CurrentFile.FullPath) `
-Action { if ($cpu -eq $null) {break}}

This will remove all breakpoints once done:

PS> Get-PSBreakpoint | Remove-PSBreakpoint

Twitter This Tip! ReTweet this Tip!