Debug School

Cover image for PowerShell Breakpoints
Suyash Sambhare
Suyash Sambhare

Posted on

PowerShell Breakpoints

Enable-PSBreakpoint

Enables the breakpoints in the current console.
The Enable-PSBreakpoint cmdlet restores disabled breakpoints. You can use it to enable all breakpoints or specific breakpoints by passing breakpoint objects or IDs. A breakpoint is a place in a script where execution is temporarily stopped to allow you to examine the script's state. Newly constructed breakpoints are automatically enabled, however they can be deactivated using Disable-PSBreakpoint. Technically, this cmdlet sets the Enabled property of a breakpoint object to True. Enable-PSBreakpoint is one of numerous cmdlets used to debug PowerShell scripts.

Samples

Enable all breakpoints
Get-PSBreakpoint | Enable-PSBreakpoint
This example enable all breakpoints in the current session.
Using aliases, this example can be shortened to gbp | ebp.

Enable breakpoints by ID
This example supports numerous breakpoints based on their breakpoint IDs.
Enable-PSBreakpoint -Id 0, 1, 5

Enable a disabled breakpoint
This example re-enables a breakpoint that has been disabled.

$B = Set-PSBreakpoint -Script "test1.ps1" -Variable Name -PassThru
$B | Enable-PSBreakpoint -PassThru

AccessMode : Write
Variable   : Name
Action     :
Enabled    : False
HitCount   : 0
Id         : 0
Script     : C:\Users\suyi\test1.ps1
ScriptName : C:\Users\suyi\test1.ps1

AccessMode : Write
Variable   : Name
Action     :
Enabled    : True
HitCount   : 0
Id         : 0
Script     : C:\Users\suyi\test1.ps1
ScriptName : C:\Users\suyi\test1.ps1
Enter fullscreen mode Exit fullscreen mode

Set-PSBreakpoint adds a breakpoint to the Name variable in the test1.ps1 script, saving the breakpoint object in the $B variable. The PassThru parameter reveals the value of the breakpoint's Enabled property, which is false. Enable-PSBreakpoint reactivates the breakpoint. Again, using the PassThru option, we can observe that the Enabled attribute is True.

PowerShell

Enable breakpoints using a variable
This example enables a set of breakpoints using the breakpoint objects.
$B = Get-PSBreakpoint -Id 3, 5
Enable-PSBreakpoint -Breakpoint $B
Get-PSBreakpoint gets the breakpoints and saves them in the $B variable. Using the Breakpoint parameter, Enable-PSBreakpoint enables the breakpoints.
This example is equivalent to running Enable-PSBreakpoint -Id 3, 5.

Enable a breakpoint in a runspace
A job is launched in this example with a breakpoint set to break and subsequently disabled. The Runspace option is used to pass the runspace, which is kept in a variable, to the Get-PSBreakPoint command. To activate the breakpoint in the runspace, the output of Get-PSBreakPoint is piped to activate-PSBreakpoint.

Start-Job -ScriptBlock {
    $bp = Set-PSBreakpoint -Command Start-Sleep
    Disable-PSBreakpoint $bp
    Start-Sleep -Seconds 10
}

$runspace = Get-Runspace -Id 1

Get-PSBreakPoint -Runspace $runspace | Enable-Breakpoint -Runspace $runspace
Enter fullscreen mode Exit fullscreen mode

PowerShell includes the following aliases for Enable-PSBreakpoint:
ebp
If you attempt to enable a breakpoint that is already enabled, the Enable-PSBreakpoint cmdlet does not produce an error. Therefore, even if only a few breakpoints are deactivated, you can reactivate all of them without encountering any issues. When you use the Set-PSBreakpoint cmdlet to establish a breakpoint, it is enabled. Enabling freshly formed breakpoints is not required.

PS C:\Users\suyi> Help Enable-PSBreakpoint

NAME
    Enable-PSBreakpoint

SYNTAX
    Enable-PSBreakpoint [-Breakpoint] <Breakpoint[]> [-PassThru] [-WhatIf] [-Confirm] [<CommonParameters>]

    Enable-PSBreakpoint [-Id] <int[]> [-PassThru] [-Runspace <runspace>] [-WhatIf] [-Confirm] [<CommonParameters>]


PARAMETERS
    -Breakpoint <Breakpoint[]>

        Required?                    true
        Position?                    0
        Accept pipeline input?       true (ByValue)
        Parameter set name           Breakpoint
        Aliases                      None
        Dynamic?                     false
        Accept wildcard characters?  false

    -Confirm

        Required?                    false
        Position?                    Named
        Accept pipeline input?       false
        Parameter set name           (All)
        Aliases                      cf
        Dynamic?                     false
        Accept wildcard characters?  false

    -Id <int[]>

        Required?                    true
        Position?                    0
        Accept pipeline input?       true (ByPropertyName)
        Parameter set name           Id
        Aliases                      None
        Dynamic?                     false
        Accept wildcard characters?  false

    -PassThru

        Required?                    false
        Position?                    Named
        Accept pipeline input?       false
        Parameter set name           (All)
        Aliases                      None
        Dynamic?                     false
        Accept wildcard characters?  false

    -Runspace <runspace>

        Required?                    false
        Position?                    Named
        Accept pipeline input?       true (ByPropertyName)
        Parameter set name           Id
        Aliases                      RunspaceId
        Dynamic?                     false
        Accept wildcard characters?  false

    -WhatIf

        Required?                    false
        Position?                    Named
        Accept pipeline input?       false
        Parameter set name           (All)
        Aliases                      wi
        Dynamic?                     false
        Accept wildcard characters?  false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, WarningVariable, OutBuffer, PipelineVariable, and OutVariable. For more information, see about_CommonParameters (https://go.microsoft.com/fwlink/?LinkID=113216).


INPUTS
    System.Management.Automation.Breakpoint[]
    System.Int32[]
    System.Management.Automation.Runspaces.Runspace


OUTPUTS
    System.Management.Automation.Breakpoint


ALIASES
    ebp


REMARKS
    Get-Help cannot find the Help files for this cmdlet on this computer. It is displaying only partial help.
        -- To download and install Help files for the module that includes this cmdlet, use Update-Help.
        -- To view the Help topic for this cmdlet online, type: "Get-Help Enable-PSBreakpoint -Online" or go to https://go.microsoft.com/fwlink/?LinkID=2096700.

PS C:\Users\suyi> Update-Help
PS C:\Users\suyi> Help Enable-PSBreakpoint

NAME
    Enable-PSBreakpoint

SYNOPSIS
    Enables the breakpoints in the current console.


SYNTAX
    Enable-PSBreakpoint [-Breakpoint] <System.Management.Automation.Breakpoint[]> [-PassThru] [-Confirm] [-WhatIf] [<CommonParameters>]

    Enable-PSBreakpoint [-Id] <System.Int32[]> [-PassThru] [-Runspace <Runspace>] [-Confirm] [-WhatIf] [<CommonParameters>]


DESCRIPTION
    The `Enable-PSBreakpoint` cmdlet re-enables disabled breakpoints. You can use it to enable all breakpoints, or specific breakpoints by providing breakpoint objects or IDs.

    A breakpoint is a point in a script where execution stops temporarily so that you can examine the state of the script. Newly created breakpoints are automatically enabled, but can be disabled using `Disable-PSBreakpoint`.

    Technically, this cmdlet changes the value of the Enabled property of a breakpoint object to True .

    `Enable-PSBreakpoint` is one of several cmdlets designed for debugging PowerShell scripts. For more information about the PowerShell debugger, see about_Debuggers (../Microsoft.PowerShell.Core/About/about_Debuggers.md).


PARAMETERS
    -Breakpoint <System.Management.Automation.Breakpoint[]>
        Specifies the breakpoints to enable. Provide a variable containing breakpoints or a command that gets breakpoint objects, such as `Get-PSBreakpoint`. You can also pipe breakpoint objects to `Enable-PSBreakpoint`.

        Required?                    true
        Position?                    0
        Default value                None
        Accept pipeline input?       True (ByValue)
        Accept wildcard characters?  false

    -Id <System.Int32[]>
        Specifies the Id numbers of the breakpoints to enable. The default value is all breakpoints. Provide the Id by number or in a variable. You can't pipe Id numbers to `Enable-PSBreakpoint`. To find the Id of a breakpoint, use the `Get-PSBreakpoint` cmdlet.

        Required?                    true
        Position?                    0
        Default value                None
        Accept pipeline input?       True (ByPropertyName)
        Accept wildcard characters?  false

    -PassThru <System.Management.Automation.SwitchParameter>
        Returns an object representing the breakpoint being enabled. By default, this cmdlet doesn't generate any output.

        Required?                    false
        Position?                    named
        Default value                False
        Accept pipeline input?       False
        Accept wildcard characters?  false

    -Runspace <Runspace>
        Specifies the Id of a Runspace object so you can interact with breakpoints in the specified runspace.

        This parameter was added in PowerShell 7.2.

        Required?                    false
        Position?                    named
        Default value                None
        Accept pipeline input?       True (ByPropertyName)
        Accept wildcard characters?  false

    -Confirm <System.Management.Automation.SwitchParameter>
        Prompts you for confirmation before running the cmdlet.

        Required?                    false
        Position?                    named
        Default value                False
        Accept pipeline input?       False
        Accept wildcard characters?  false

    -WhatIf <System.Management.Automation.SwitchParameter>
        Shows what would happen if the cmdlet runs. The cmdlet isn't run.

        Required?                    false
        Position?                    named
        Default value                False
        Accept pipeline input?       False
        Accept wildcard characters?  false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, WarningVariable, OutBuffer, PipelineVariable, and OutVariable. For more information, see about_CommonParameters (https://go.microsoft.com/fwlink/?LinkID=113216).

INPUTS
    System.Management.Automation.Breakpoint
        You can pipe a breakpoint object to this cmdlet.


OUTPUTS
    None
        By default, this cmdlet returns no output.

    System.Management.Automation.Breakpoint
        When you use the PassThru parameter, this cmdlet returns a breakpoint object representing the enabled breakpoint.


NOTES


        PowerShell includes the following aliases for `Enable-PSBreakpoint`:

        - All platforms:   - `ebp`

        - The `Enable-PSBreakpoint` cmdlet doesn't generate an error if you try to enable a breakpoint that   is already enabled. As such, you can enable all breakpoints without error, even when only a few are disabled.

        - Breakpoints are enabled when you create them by using the `Set-PSBreakpoint` cmdlet. You don't   need to enable newly created breakpoints.

    -------------- Example 1: Enable all breakpoints --------------

    Get-PSBreakpoint | Enable-PSBreakpoint

    Using aliases, this example can be abbreviated as `gbp | ebp`.
    ------------- Example 2: Enable breakpoints by ID -------------

    Enable-PSBreakpoint -Id 0, 1, 5


    ----------- Example 3: Enable a disabled breakpoint -----------

    $B = Set-PSBreakpoint -Script "sample.ps1" -Variable Name -PassThru
    $B | Enable-PSBreakpoint -PassThru

    AccessMode : Write
    Variable   : Name
    Action     :
    Enabled    : False
    HitCount   : 0
    Id         : 0
    Script     : C:\ps-test\sample.ps1
    ScriptName : C:\ps-test\sample.ps1

    AccessMode : Write
    Variable   : Name
    Action     :
    Enabled    : True
    HitCount   : 0
    Id         : 0
    Script     : C:\ps-test\sample.ps1
    ScriptName : C:\ps-test\sample.ps1

    `Set-PSBreakpoint` creates a breakpoint on the Name variable in the `Sample.ps1` script saving the breakpoint object in the `$B` variable. The PassThru parameter displays the value of the Enabled property of the breakpoint is False .

    `Enable-PSBreakpoint` re-enables the breakpoint. Again, using the PassThru parameter we see that the value of the Enabled property is True .
    -------- Example 4: Enable breakpoints using a variable --------

    $B = Get-PSBreakpoint -Id 3, 5
    Enable-PSBreakpoint -Breakpoint $B

    `Get-PSBreakpoint` gets the breakpoints and saves them in the `$B` variable. Using the Breakpoint parameter, `Enable-PSBreakpoint` enables the breakpoints.

    This example is equivalent to running `Enable-PSBreakpoint -Id 3, 5`.
    --------- Example 5: Enable a breakpoint in a runspace ---------

    Start-Job -ScriptBlock {
        $bp = Set-PSBreakpoint -Command Start-Sleep
        Disable-PSBreakpoint $bp
        Start-Sleep -Seconds 10
    }

    $runspace = Get-Runspace -Id 1

    Get-PSBreakPoint -Runspace $runspace | Enable-Breakpoint -Runspace $runspace



RELATED LINKS
    Online Version: https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/enable-psbreakpoint?view=powershell-7.3&WT.mc_id=ps-gethelp
    Disable-PSBreakpoint
    Get-PSBreakpoint
    Get-PSCallStack
    Remove-PSBreakpoint
    Set-PSBreakpoint

Enter fullscreen mode Exit fullscreen mode

Ref: https://learn.microsoft.com/en-gb/powershell/module/microsoft.powershell.utility/enable-psbreakpoint

Top comments (0)