TExcelTAD
Home › PowerShell › PowerShell Output Streams

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:

powershell · try it yourself
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

powershell · try it yourself
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 -Verbose
WARNING: 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).

powershell · try it yourself
$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.

powershell · try it yourself
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

powershell · try it yourself
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 Continue
Out-String gave a String: a.txt
shown on request

Tip: Never use Write-Host to 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 = ...?

  1. Write-Host 'hi'
  2. Write-Output 'hi'
  3. Write-Warning 'hi'
  4. Write-Verbose 'hi'

How do you hide the error messages of one command?

  1. Append 2>$null
  2. Append >null
  3. Use Write-Host
  4. Append | Out-Error

When are Write-Verbose messages shown by default?

  1. Always
  2. Never
  3. Only when -Verbose is used or $VerbosePreference is Continue
  4. Only in scripts