PowerShell Commands

Set-PSDebug

Set-PSDebug [-Off] [<CommonParameters>]
Set-PSDebug [-Step] [-Strict] [-Trace <Int32>] [<CommonParameters>]

The Set-PSDebug cmdlet turns script debugging features on and off, sets the trace level, and toggles strict mode.

When the Trace parameter has a value of 1, each line of script is traced as it runs. When the parameter has a value of 2, variable assignments, function calls, and script calls are also traced. If the Step parameter is specified, you are prompted before each line of the script runs.

Parameters

-Off [<SwitchParameter>]

  • Default value is False
  • Accepts pipeline input False

Indicates that this cmdlet turns off all script debugging features.

A `Set-StrictMode -Off` command disables the verification set by a `Set-PSDebug -Strict` command.

-Step [<SwitchParameter>]

  • Default value is False
  • Accepts pipeline input False

Indicates that this cmdlet turns on script stepping. Before each line runs, Windows PowerShell prompts you to stop, continue, or enter a new interpreter level to inspect the state of the script.

Specifying the Step parameter automatically sets a trace level of 1.

-Strict [<SwitchParameter>]

  • Default value is False
  • Accepts pipeline input False

Indicates that Windows PowerShell returns an exception if a variable is referenced before a value is assigned to the variable.

A `Set-StrictMode -Off` command disables the verification set by a `Set-PSDebug -Strict` command.

-Trace <Int32>

  • Default value is None
  • Accepts pipeline input False

Specifies the trace level. The acceptable values for this parameter are:

- 1: Trace script lines as they run.

- 0: Turn script tracing off.

- 2: Trace script lines, variable assignments, function calls, and scripts.

<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,ErrorAction, ErrorVariable, WarningAction, WarningVariable,OutBuffer, PipelineVariable, and OutVariable.

Inputs
None
You cannot pipe input to this cmdlet.
Outputs
None
This cmdlet does not return any output.
Examples
  1. Set the trace level to 2:
    PS C:\> Set-PSDebug -Trace 2; foreach ($i in 1..3) {$i}
    
       DEBUG:    1+ Set-PsDebug -Trace 2; foreach ($i in 1..3) {$i}
       DEBUG:    1+ Set-PsDebug -Trace 2; foreach ($i in 1..3) {$i}
       1
       DEBUG:    1+ Set-PsDebug -Trace 2; foreach ($i in 1..3) {$i}
       2
       DEBUG:    1+ Set-PsDebug -Trace 2; foreach ($i in 1..3) {$i}
       3

    This command sets the trace level to 2, and then runs a script that displays the numbers 1, 2, and 3.

  2. Turn on stepping:
    PS C:\> Set-PSDebug -Step; foreach ($i in 1..3) {$i}
    
       DEBUG:    1+ Set-PsDebug -Step; foreach ($i in 1..3) {$i}
       Continue with this operation?
       1+ Set-PsDebug -Step; foreach ($i in 1..3) {$i}
       [Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help
       (default is "Y"):a
       DEBUG:    1+ Set-PsDebug -Step; foreach ($i in 1..3) {$i}
       1
       2
       3

    This command turns on stepping, and then runs a script that displays the numbers 1, 2, and 3.

  3. Turn off debug features:
    PS C:\> Set-PSDebug -Off; foreach ($i in 1..3) {$i}
    1
    2
    3
    

    This command turns off all debugging features, and then runs a script that displays the numbers 1, 2, and 3.

  4. Use strict mode:
    PS C:\> set-psdebug -Strict; $NewVar
    The variable $NewVar cannot be retrieved because it has not been set yet.
    At line:1 char:28
    + Set-PsDebug -strict;$NewVar <<<<
    

    This command puts Windows PowerShell in strict mode, and then attempts to access a variable that has not yet been set.

Additional Notes

This work is licensed under a Creative Commons Attribution 4.0 International. It is attributed to Microsoft Corporation and can be found here.

PowerShell Commands