🎓 A1Learner
🔍
0 of 46 lessons completed

PowerShell Breakpoints

A breakpoint is a marker that tells PowerShell to pause at a chosen place. The marker can sit on a function, and this lesson sets one on a function and lists it.

Set a breakpoint on a function

Name the function when you set the breakpoint. The count line confirms that one breakpoint is set.

function Get-Double { param([int]$n) $n * 2 }
$breakpoint = Set-PSBreakpoint -Command "Get-Double"
(Get-PSBreakpoint).Count

List the breakpoint by its function name

Get-PSBreakpoint lists what is set. Asking for the command name prints a plain value.

function Get-Double { param([int]$n) $n * 2 }
$breakpoint = Set-PSBreakpoint -Command "Get-Double"
Get-PSBreakpoint | Select-Object -ExpandProperty Command

Remove the breakpoint

Pass the stored breakpoint to the remove command. The count line confirms that none remain.

function Get-Double { param([int]$n) $n * 2 }
$breakpoint = Set-PSBreakpoint -Command "Get-Double"
Remove-PSBreakpoint -Breakpoint $breakpoint
(Get-PSBreakpoint).Count

Example

function Get-Double { param([int]$n) $n * 2 }
$breakpoint = Set-PSBreakpoint -Command "Get-Double"
"before remove: $((Get-PSBreakpoint).Count)"
Remove-PSBreakpoint -Breakpoint $breakpoint
"after remove: $((Get-PSBreakpoint).Count)"
Get-Double -n 21

Key points

  • Breakpoint: a marker that tells PowerShell to pause at a chosen place.
  • Use Set-PSBreakpoint to place a breakpoint on a function.
  • Use Get-PSBreakpoint to list the breakpoints that are set.
  • Use Remove-PSBreakpoint to clear a breakpoint you no longer need.
  • Use (Get-PSBreakpoint).Count to check how many breakpoints are set.

In this example, a breakpoint is set on the function, the count shows one, the breakpoint is removed, the count shows zero, and the function then runs normally and returns its doubled value.

Note

A breakpoint stays set until you remove it, even after the function runs.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

You set a breakpoint on a function and want a stable check that it exists. What do you print?