Enable-PSBreakpoint

Exploring PowerShell Cmdlets: Enable-PSBreakpoint

Welcome back to Wahmans PowerShell blog! Today, we’re digging into a useful debugging tool in PowerShell: the Enable-PSBreakpoint cmdlet. Debugging scripts can be a headache, but with breakpoints, you get a chance to inspect what your code is doing mid-execution. The Enable-PSBreakpoint cmdlet enables breakpoints that have been set but might be disabled. This functions within the console session, allowing for efficient, interactive debugging.

What is Enable-PSBreakpoint?

According to Microsoft, Enable-PSBreakpoint “Enables the breakpoints in the current console.” If you’ve previously set a breakpoint using Set-PSBreakpoint or New-PSBreakpoint (or seen one created while debugging), there may be times where a breakpoint is disabled. This cmdlet is your key to reactivating it without clearing and recreating breakpoints.

Let’s take a look at some examples

Example 1: Set and Enable a Breakpoint by Line – Beginner Level

# Create a sample script
$script = @'
function Say-Hello {
    "Step 1"
    "Step 2"
    "Step 3"
}

Say-Hello
'@
Set-Content -Path ./DemoScript.ps1 -Value $script

# Add a breakpoint at line 4
$bp = Set-PSBreakpoint -Script ./DemoScript.ps1 -Line 4

# Disable the breakpoint
Disable-PSBreakpoint -Id $bp.Id

# Re-enable it using Enable-PSBreakpoint
Enable-PSBreakpoint -Id $bp.Id

This beginner-friendly example shows how to control breakpoints manually via their ID. It’s a great first step into interactive debugging in PowerShell.

Example 2: Enable All Breakpoints in the Console Session

# View all breakpoints
Get-PSBreakpoint

# Enable all currently disabled breakpoints
Get-PSBreakpoint | Where-Object { !$_.Enabled } | Enable-PSBreakpoint

Here, we’re filtering to find all the breakpoints that are disabled and re-enabling them in one sweep. This is helpful when you have large scripts with scattered breakpoints.

Example 3: Enable Breakpoint by Variable Watch – Intermediate

# Set a breakpoint on a variable assignment
$bp = Set-PSBreakpoint -Variable "myVar" -Mode Write

# Start script that changes the variable
Start-Job -ScriptBlock {
    $global:myVar = 10
}

# Disable then re-enable the breakpoint
Disable-PSBreakpoint -Id $bp.Id
Enable-PSBreakpoint -Id $bp.Id

This helps in watching how and when variables change — a critical part of debugging logic bugs or tracing data flow.

Example 4: Enable Breakpoints in a Debugging Workflow – Advanced

# Define a complex function with multiple breakpoints
function Test-Loop {
    for ($i = 0; $i -lt 5; $i++) {
        "Iteration: $i"
    }
}

# Save the function to a script
$functionContent = (Get-Command Test-Loop).Definition
Set-Content -Path ./ComplexScript.ps1 -Value $functionContent

# Set multiple breakpoints
$breakpoints = @(
    Set-PSBreakpoint -Script ./ComplexScript.ps1 -Line 3,
    Set-PSBreakpoint -Script ./ComplexScript.ps1 -Line 4
)

# Disable all
$breakpoints | Disable-PSBreakpoint

# Enable selectively
Enable-PSBreakpoint -Id $breakpoints[1].Id

This last example demonstrates enabling specific breakpoints where needed, tuning your debugging flow to uncover specific bugs in loops and logic control.

Wrapping Up

Debugging in PowerShell is supercharged by breakpoints, and Enable-PSBreakpoint is a handy cmdlet that brings flexibility when managing them. Use it in interactive sessions, automation workflows, and even inside modular scripts to precisely control where and when your code halts for inspection.

Happy scripting, and I will see you in the next post!

Leave a Reply

Your email address will not be published. Required fields are marked *