Understanding Get-PSBreakpoint in PowerShell
Welcome back to Wahmans PowerShell blog! Today, we’re diving into a powerful debugging cmdlet that sometimes hides in plain sight: Get-PSBreakpoint.
As Microsoft describes it, Get-PSBreakpoint “gets the breakpoints that are set in the current session.” This is useful when you’re debugging scripts and need an overview of your current breakpoints.
What is a Breakpoint?
In PowerShell, a breakpoint is a marker that tells the script to pause execution when a certain line, command, or variable is hit—allowing the developer to inspect and debug the code.
Usage Scenarios
Let’s explore four examples of how Get-PSBreakpoint can be used—from beginner all the way to more advanced use cases.
🔰 Example 1: Viewing All Breakpoints (Beginner)
Simply want to check which breakpoints are currently set? Use Get-PSBreakpoint with no parameters.
Get-PSBreakpoint
This will return a list of all breakpoints you’ve currently set in your session—line, command, or variable breakpoints.
🔰 Example 2: Set a Breakpoint and Confirm It (Beginner+)
Create a basic line breakpoint and inspect it using Get-PSBreakpoint.
# Set a line breakpoint on line 3 of example.ps1
Set-PSBreakpoint -Script "example.ps1" -Line 3
# View the created breakpoint
Get-PSBreakpoint
🔧 Example 3: Filter Breakpoints by Script (Intermediate)
Suppose you’re working with multiple scripts and only want to inspect breakpoints on a specific one.
# Set breakpoints in two scripts
Set-PSBreakpoint -Script "scriptA.ps1" -Line 5
Set-PSBreakpoint -Script "scriptB.ps1" -Line 8
# Only get breakpoints from scriptA.ps1
Get-PSBreakpoint | Where-Object { $_.Script -eq "scriptA.ps1" }
⚙️ Example 4: Conditionally Remove All Command Breakpoints (Advanced)
Sometimes you might want to remove only command breakpoints. Let’s inspect them first and then remove if needed.
# View only command breakpoints
$cmdBreakpoints = Get-PSBreakpoint | Where-Object { $_.BreakpointType -eq 'Command' }
# Display
$cmdBreakpoints
# Remove them
$cmdBreakpoints | Remove-PSBreakpoint
Final Thoughts
Knowing how to work with breakpoints efficiently helps you take full control of script debugging. Get-PSBreakpoint provides you the transparency you need when suspending execution mid-script and tracking your troubleshooting process.
Happy scripting, and I will see you in the next post!
Leave a Reply