Troubleshooting PowerShell Scripts with Set-PSBreakPoint

The Set-PSBreakPoint cmdlet sets a breakpoint in a script. When you are troubleshooting a script it could be helpful to know what’s going on in a particualar step or workflow. In this blog bost I will give you an overview and the basics you can build on to troubleshoot and investigate your script. Let’s jump in.

Starting Point

The starting point for this topic is this simple PowerShell script.

for ($i = 100; $i -ge 0; $i -= 3) { Write-Host $i }

Short explanation: $i is set to 100. The script counts down 3 from 100 until $i is greater or eqal 0.


The script is saved as a ps1 file in C:\Temp.

Investigating Lines

Now Set-PSBreakPoint comes into play.

Our next target is to find out what value $i returns in Line 2. It should return 100. Let’s check it out with Set-PSBreakPoint and the Line parameter.

Set-PSBreakpoint -Script 'C:\Temp\for_5.ps1' -Line 2

After setting the breakpoint, we need to start the script to get our Breakpoint into action.


Wow, there is a hit … and the hit is highlighted by PowerShell ISE … what a nice screen!


Ok, now let’s get back to our main target. What is the value of $i? Yeah, it’s 100.


To start from the scratch in the next part, I stop the debugger by pressing SHIFT + F5 and then I remove all PSBreakPoints.

Get-PSBreakpoint | Remove-PSBreakpoint

Investigating Variables

Now we will go one further. Let’s suppose we want to know what $i returns when $i is less than 50. In order to do this, we have to define the Variable and the Action parameter.

Set-PSBreakpoint -Script 'C:\Temp\for_5.ps1' -Variable i -Action { if ($i -lt 50) { break } }

Nice. I have to explain that. The variable parameter is set to i, which actually means $i. The Action parameter contains an if statement. The if statement is set to $i -lt (less than) 50 and the break statement stops the execution of the script when the condition (lt 50) is met.

Let’s give it a try!


If the condition is met $i is 49, not 50. Why? Remember the script counts down by 3.

By the way, Set-PSBreakpoint can also set breakpoints on commands and functions.

More here: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/set-psbreakpoint?view=powershell-6

Categories: PowerShell

2 replies »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.