Set-PSDebug

 

SYNOPSIS

Turns script debugging features on and off, sets the trace level and toggles strict mode.

 

SYNTAX

Set-PSDebug [-trace <int>] [-step] [-strict] [<CommonParameters>]

 

Set-PSDebug [-off] [<CommonParameters>]

 

DETAILED DESCRIPTION

Turns script debugging features on and off, sets the trace level and toggles strict mode. This cmdlet allows you to control the script debugging options of the Windows PowerShell interpreter. When the Trace parameter is set to 1, only the line of script to be executed will be emitted. When the parameter is set to 2, variable assignments, function and script calls will also be traced.

 

If the Step parameter is specified, the user will be presented with the same choices as the Confirm parameter before each line of the script is executed.

 

The behavior of the system in response to the confirmation prompt in step mode is as follows:

 

·          "Yes" executes the single line

·          "Yes to All" exits step mode (the script continues to run with no confirmation prompts)

·          "No" and "No To All" exit the script

·          "Suspend" launches a nested shell and then returns to the confirmation prompt when the nested shell exits.

 

PARAMETERS

 

-trace <int>

Specifies the trace level:

 

·          0 - turn script tracing off

·          1 - trace script lines as they are executed

·          2 - trace script lines, variable assignments, function calls and scripts.

 

Required?

false

Position?

named

Default value

1

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-step <SwitchParameter>

Turns on script stepping. Before each line is run, the user is prompted to stop, continue or enter a new interpreter level to inspect the state of the script.

 

Note that specifying Step automatically sets a Trace level of 1.

 

Required?

false

Position?

named

Default value

False

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-off <SwitchParameter>

Turns off all script debugging features.

 

Required?

false

Position?

named

Default value

False

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-strict <SwitchParameter>

Specfies that the interpreter should throw an exception if a variable is referenced before being assigned a value.

 

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, and -OutVariable. For more information, type, "get-help about_commonparameters".

 

NOTES

 

For more information, type "Get-Help Set-PsDebug  -detailed". For technical information, type "Get-Help Set-PsDebug -full".

 

When specifying multiple values for a parameter, use commas to separate the values. For example, "<parameter-name> <value1>, <value2>".

 

EXAMPLE 1

 

set-psdebug -trace 2; foreach ($i in 1..3) {$i}

 

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

 

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

 

EXAMPLE 2

 

set-psdebug -step; foreach ($i in 1..3) {$i}

 

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

 

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

 

EXAMPLE 3

 

set-psdebug -off; foreach ($i in 1..3) {$i}

 

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

 

1

2

3

 

EXAMPLE 4

 

set-psdebug -strict; $NewVar

 

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

 

The variable $NewVar cannot be retrieved because it has not been set yet.

 

At line:1 char:28

+ Set-PsDebug -strict;$NewVar <<<<

 

RELATED LINKS

Write-Debug