PowerShell Verbose and diagnostic streams
PowerShell separates ordinary data from diagnostic and informational messages by using numbered streams. The success stream is stream 1, errors are stream 2, warnings stream 3, verbose stream 4, debug stream 5, and information stream 6.
Documentation: Microsoft Learn
Overview
Write-Verbose and Write-Debug are intended for diagnostics and are normally gated by -Verbose and -Debug. Write-Information writes to stream 6 and can carry strings or objects. Since Windows PowerShell 5.0, Write-Host is implemented on the information stream as well, so its messages can be redirected or captured with information-stream redirection. The important distinction is that Write-Host is still not ordinary success-stream output: a function should not use it as the data contract that callers are expected to pipe, filter, sort, or export.
For reusable functions, emit result objects on the success stream and reserve verbose, debug, warning, error, and information messages for commentary about the operation. That gives the caller control over what to display, suppress, redirect, or log.
Inspect this example’s structure
Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.
function Show-Streams {
[CmdletBinding()]
param()
Write-Output 'Output: the only stream that goes down the pipeline'
Write-Verbose 'Verbose: hidden unless -Verbose is passed'
Write-Warning 'Warning: shown by default, not part of output'
}
Show-Streams -Verbose- Stage 1function
-Verbose
An example to inspect
Preview the example. Sign in for the full lesson and activities.
function Show-Streams {
[CmdletBinding()]
param()
Write-Output 'Output: the only stream that goes down the pipeline'
Write-Verbose 'Verbose: hidden unless -Verbose is passed'
Write-Warning 'Warning: shown by default, not part of output'
}
Show-Streams -VerboseWhat happens
The function writes to three streams. Called with -Verbose, all three appear, and the verbose line is prefixed with VERBOSE: while the warning is prefixed WARNING:. Drop -Verbose and the verbose line vanishes while the other two stay. Only the Write-Output line would survive being assigned to a variable.
Worth remembering
- Success-stream objects are the normal pipeline data; diagnostic and informational messages use separate streams.
- Write-Verbose and Write-Debug are opt-in diagnostics controlled by -Verbose and -Debug.
- Since Windows PowerShell 5.0, Write-Host uses the information stream and can be redirected or captured separately; it is still not a substitute for returning result objects on stream 1.
- Design functions so callers can choose independently what data to consume and what diagnostics to display or log.
Primary documentation
Try it
Illustrative output · values vary by environment
Example reader
Inspect this page's example
Run supported commands against the practice data, or inspect the example without executing it.
Ready to inspect.
Writes to three different streams so you can see which ones appear.
Show reference output
Illustrative output · values vary by environment
Output: the only stream that goes down the pipeline VERBOSE: Verbose: hidden unless -Verbose is passed WARNING: Warning: shown by default, not part of output
Practise this
Inspect warning diagnostics
Return Id and Message for Warning-level events.
The sandbox is visible now. Sign in with Google only when you run or check a task; commands and temporary files are discarded when you leave.
Run a command to inspect its result.
Inspect starting data
Supported syntax
Virtual files
Keep going
Next: PowerShell CIM versus WMICheck your understanding
How can you add diagnostic messages without mixing them with report objects?
Use Write-Verbose for diagnostics and enable them with -Verbose on an advanced function. Return report objects on the success stream.