Skip to content

Write-Host output is invisible to 2>&1 | Out-String

streams · case

bothsilentscriptcifirst-partyrepro: verifiedhost-vs-pipeline
Affectspowershell 51, pwsh 7
Fails assilent
Mechanismhost vs pipeline
Safe fixwrite output

A test captures “everything” with 2>&1 | Out-String — and the capture is EMPTY, though the script clearly prints when run interactively. Assertions fail against a blank string.

Terminal window
$out = & { Write-Host "important message" } 2>&1 | Out-String
$out.Length # 0 — the message went to the host, not the pipeline

Write-Host writes to the HOST, not the success stream. 2>&1 merges stderr only; the message never enters the captured pipeline. In-process capture of host output needs a different observer entirely.

  • Script authors: Write-Output for capturable content; Write-Host only for human-only chrome.
  • Test authors: 6>&1 merges the information stream, or wrap the run in Start-Transcript / Stop-Transcript (the referenced fix) to observe host output reliably.