🎓 A1Learner
🔍
0 of 46 lessons completed

PowerShell Debug Output

The debug stream is a separate channel for extra detail that helps find a fault. Write-Debug writes to that channel, and the detail stays hidden until you ask for it.

Write-Debug writes a detail message

The preference is set to Continue first. The debug message then prints.

$DebugPreference = 'Continue'
Write-Debug "starting the total"
"done"

Without Continue, the message stays hidden

With SilentlyContinue, the same message stays hidden. Only the normal output prints.

$DebugPreference = 'SilentlyContinue'
Write-Debug "starting the total"
"done"

Debug messages inside a function show its steps

A function can report its own steps. The debug message prints before the result.

$DebugPreference = 'Continue'
function Add-Numbers { param([int]$a, [int]$b) Write-Debug "adding $a and $b"; $a + $b }
Add-Numbers -a 3 -b 4

Example

$DebugPreference = 'Continue'
function Get-Sum { param([int]$a, [int]$b) Write-Debug "inputs: $a, $b"; $total = $a + $b; Write-Debug "total: $total"; $total }
Get-Sum -a 6 -b 7

Key points

  • Debug stream: a separate channel for extra detail while code runs.
  • Use Write-Debug to write a short detail message to the debug stream.
  • Use $DebugPreference = 'Continue' to show debug messages.
  • Default value: $DebugPreference starts as SilentlyContinue, so debug messages stay hidden.
  • A debug message prints with a DEBUG label in front of the text.

In this example, Get-Sum reports its inputs, works out the total, then reports the total. The final line is the function result, and the two DEBUG lines are the detail around it.

Note

normal output still prints as usual, with or without debug messages.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

You set $DebugPreference to Continue and run Write-Debug 'checking'. What prints?