Write-Host vs Write-Output: What’s the Difference?
In PowerShell, Write-Host and Write-Output are often mistaken for interchangeable commands.
This post is a companion for the video embedded below. Scroll down to see the code from the video.
Side-by-Side Comparison
Write-Host "Hello from Write-Host"
Write-Output "Hello from Write-Output"
At first glance, they both display text to the screen. However:
Write-Hostsends output directly to the console.Write-Outputsends output to the PowerShell pipeline.
Formatting with Write-Host
Write-Host "Starting process..." -ForegroundColor Cyan
Write-Host shines when you want to provide user-friendly, color-coded feedback without affecting your script’s output or logic.
Capturing Write-Output
$output = Write-Output "Process output"
$output
Write-Output returns the string and allows it to be assigned, manipulated, or passed to another command.
Capturing Write-Host
$outhost = Write-Host "Process host"
$outhost
The variable $outhost will be $null. Write-Host is purely for display and does not return a value.
Function Output with Write-Output
function Test-Output {
Write-Output "I'm writing to the pipeline"
}
$outputMsg = Test-Output
$outputMsg
$outputMsg.ToUpper()
Because Write-Output returns the string, we can call methods like ToUpper() on the result.
Function Output with Write-Host
function Test-Host {
Write-Host "I'm writing to the host"
}
$hostMsg = Test-Host
$hostMsg
In this case, the text is displayed, but $hostMsg will be $null. The function doesn’t return anything usable in the pipeline.
Multiple Outputs
function Test-MultiOutput {
Write-Output "Out 1"
Write-Output "Out 2"
}
$outputMsg = Test-MultiOutput
$outputMsg
Write-Output allows multiple values to be returned and handled downstream.
Logging: Be Intentional
Write-Host "Logged event"
Write-Verbose "This is a verbose message"
While Write-Host displays the message, it cannot be redirected or suppressed. For logging purposes, consider Write-Verbose which respects the $VerbosePreference setting and can be toggled on or off.
Summary
Write-Hostwrites only to the consoleWrite-Outputsends data to the pipelineWrite-Hostis great for status messages and formatted textWrite-Outputis better for returning data in functions or scriptsWrite-Hostreturns$null;Write-Outputreturns the object- Use
Write-VerboseorWrite-Informationfor logging
Conclusion
Both Write-Host and Write-Output are useful, but for different purposes. If you’re communicating with the user, use Write-Host. If you’re communicating with your script or other cmdlets, use Write-Output. Understanding the difference will help you write more robust, reusable, and predictable scripts.