PowerShell Output Streams
PowerShell doesn't have just one "output". It has six numbered streams, each for a different kind of message. Knowing them explains why some text can be piped and some can't, and how to keep scripts quiet or chatty on demand.
| # | Stream | Write with | Shown by default? |
|---|---|---|---|
| 1 | Success (output) | Write-Output, or just a value |
yes, and it goes down the pipeline |
| 2 | Error | Write-Error, failing cmdlets |
yes |
| 3 | Warning | Write-Warning |
yes (WARNING: prefix) |
| 4 | Verbose | Write-Verbose |
no, only with -Verbose |
| 5 | Debug | Write-Debug |
no, only with -Debug |
| 6 | Information | Write-Information, Write-Host |
Write-Host yes, Write-Information no |
Output vs Write-Host
Only the success stream is passed down the pipeline or captured into a variable. Write-Host writes to the information stream, which goes to the screen:
function Get-Answer {
Write-Host 'Calculating...'
42
}
$result = Get-Answer
"Captured: $result"Calculating...
Captured: 42
Calculating... was shown on the screen but not stored in $result. Rule of thumb: data goes to the output, messages for humans go to Write-Host, Write-Verbose or Write-Warning.
Warnings and verbose messages
function Remove-OldBackup {
[CmdletBinding()]
param([int]$Days = 30)
Write-Verbose "Looking for backups older than $Days days"
if ($Days -lt 7) {
Write-Warning 'Very short retention period!'
}
"Done (retention $Days days)"
}
Remove-OldBackup -Days 3
'---'
Remove-OldBackup -Days 60 -VerboseWARNING: Very short retention period!
Done (retention 3 days)
---
VERBOSE: Looking for backups older than 60 days
Done (retention 60 days)
[CmdletBinding()] turns a function into an advanced function, which gives it -Verbose, -Debug and the other common parameters for free. Verbose messages stay hidden until the user asks for them, so you can sprinkle them generously.
Preference variables
Each stream has a preference variable that decides what happens to its messages: Continue (show), SilentlyContinue (hide), Stop (turn into an error) or Inquire (ask).
$VerbosePreference = 'Continue'
Write-Verbose 'now visible without -Verbose'
$VerbosePreference = 'SilentlyContinue'
Write-Verbose 'hidden again'
$WarningPreference = 'SilentlyContinue'
Write-Warning 'you will not see this'
$WarningPreference = 'Continue'
Write-Warning 'but this one is shown'VERBOSE: now visible without -Verbose
WARNING: but this one is shown
| Variable | Default |
|---|---|
$ErrorActionPreference |
Continue |
$WarningPreference |
Continue |
$VerbosePreference |
SilentlyContinue |
$DebugPreference |
SilentlyContinue |
$InformationPreference |
SilentlyContinue |
Redirection
Use > to send a stream to a file (overwrite), >> to append, and 2>&1-style syntax to merge streams. The stream numbers come from the table above; * means all streams.
function Test-Streams {
'regular output'
Write-Warning 'a warning'
Write-Error 'an error'
}
Test-Streams 2>$null 3>$null # drop errors and warnings
'---'
Test-Streams 3> ./warnings.txt 2>$null # warnings into a file
"File holds: $(Get-Content ./warnings.txt)"
'---'
$captured = Test-Streams 3>&1 2>&1 # merge into the output stream
$captured | ForEach-Object { $_.GetType().Name }regular output
---
regular output
File holds: a warning
---
String
WarningRecord
ErrorRecord
2>$null is the common way to silence error messages of a single command. *> file.txt sends all streams to a file. Merging with 2>&1 makes warning and error records ordinary pipeline objects (WarningRecord, ErrorRecord), so you can store or inspect them.
Out-Null, Out-String, Write-Information
New-Item -ItemType Directory -Path ./scratch -Force | Out-Null # hide output
[void](New-Item -ItemType File -Path ./scratch/a.txt -Force) # same effect
$text = Get-ChildItem ./scratch | Select-Object -ExpandProperty Name | Out-String
"Out-String gave a $($text.GetType().Name): $($text.Trim())"
Write-Information 'hidden by default'
Write-Information 'shown on request' -InformationAction ContinueOut-String gave a String: a.txt
shown on request
Tip: Never use
Write-Hostto return data from a function. Callers can't capture it. Output the value instead and let them decide how to show it.
Test yourself
Which command's text can be stored in a variable with $x = ...?
- Write-Host 'hi'
- Write-Output 'hi'
- Write-Warning 'hi'
- Write-Verbose 'hi'
How do you hide the error messages of one command?
- Append 2>$null
- Append >null
- Use Write-Host
- Append | Out-Error
When are Write-Verbose messages shown by default?
- Always
- Never
- Only when -Verbose is used or $VerbosePreference is Continue
- Only in scripts