Set PSBreakpoint - PowerShell


Overview

Set-PSBreakpoint is a PowerShell command used to set breakpoints in the current script or module. Breakpoints allow you to pause script execution at a specific point and inspect the script’s state. This is valuable for debugging and understanding script behavior.

Syntax

Set-PSBreakpoint [-BreakOnEnter] [-BreakOnExit] [-BreakOnThrow] [-Command <string>]
[-Condition <string>] [-EndAtLine <Int32>] [-Erase] [-LineNumber <Int32>]
[-Scope <string>] [-Script <PSObject>] [-StartAtLine <Int32>] [-Title <string>]
[-Expression <string>] [-PassThru]

Options/Flags

  • -BreakOnEnter: Pauses execution when entering the specified breakpoint.
  • -BreakOnExit: Pauses execution when exiting the specified breakpoint.
  • -BreakOnThrow: Pauses execution when an exception is thrown at the specified breakpoint.
  • -Command: Sets a breakpoint on a specific command.
  • -Condition: Specifies a condition that must be met for the breakpoint to trigger.
  • -EndAtLine: Sets the ending line number for the breakpoint range.
  • -Erase: Removes all existing breakpoints.
  • -LineNumber: Sets a breakpoint at the specified line number.
  • -Scope: Sets the scope of the breakpoint (script, module, global).
  • -Script: Sets a breakpoint in the specified script.
  • -StartAtLine: Sets the starting line number for the breakpoint range.
  • -Title: Sets a title for the breakpoint.
  • -Expression: Pauses the execution when the result of an expression is evaluated as True.
  • -PassThru: Returns the created breakpoint object.

Examples

Simple breakpoint:

Set-PSBreakpoint -LineNumber 10

Breakpoint with a condition:

Set-PSBreakpoint -LineNumber 10 -Condition { $i -gt 10 }

Breakpoint in a module:

Set-PSBreakpoint -Scope Module -Script Get-Date -LineNumber 1

Common Issues

  • Breakpoints may not trigger if the debugger is not enabled. To enable the debugger, run PowerShell as “PowerShell ISE (x64)” or use the -EnableDebugger flag.
  • Breakpoints may not trigger if the script is running in a different execution context, such as a background job.

Integration

With the Debug-Script cmdlet:

Debug-Script -ScriptBlock {
    Set-PSBreakpoint -LineNumber 10
    ...
}
  • Get-PSBreakpoint: Gets existing breakpoints.
  • Remove-PSBreakpoint: Removes breakpoints.
  • Debug-Script: Starts a debugging session for a script.