18.3. The write-debug Cmdlet

The write-debug cmdlet writes a message to the Windows PowerShell console. It differs from the write-host cmdlet in that the effect of the write-debug cmdlet is controlled by the value of the $DebugPreference variable. In addition to the common parameters, the write-debug cmdlet supports a single parameter:

  • Message — The debug message to be sent to the console

The Message parameter is a required parameter and is also a positional parameter at position 1.

The file AdditionWithDebug.ps1 has several write-debug statements that make it clear to the user which part of the script has been entered:

write-debug "Script addition.ps1 has been entered."
$a = 10
Write-debug "Variable $a has been assigned"
$b = 7
Write-debug "Variable $a has been assigned"
write-host "The sum of $a + $b is $($a+$b)"
write-debug "Execution of script addition.ps1 has been completed."'

The default setting of the $DebugPreference variable is SilentlyContinue. If you want to see the results of write-debug statements displayed, set the value of the $DebugPreference variable to Continue.

$DebugPreference = "Continue"

With the value of $DebugPreference set to Continue, Figure 18-14 shows the results of executing the script AdditionWithDebug.ps1. The set-psdebug setting was Off.

Figure 18.14. Figure 18-14

If you set $DebugPreference to Stop, then execution of a script will stop after the ...

Get Professional Windows® PowerShell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.