Write-Host output is invisible to 2>&1 | Out-String
streams · case
Affectspowershell 51, pwsh 7
Fails assilent
Mechanismhost vs pipeline
Safe fixwrite output
Symptom
Section titled “Symptom”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.
$out = & { Write-Host "important message" } 2>&1 | Out-String$out.Length # 0 — the message went to the host, not the pipelineWrite-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.
Workaround
Section titled “Workaround”- 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.