PoshBytes: Write-Host vs Write-Output

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-Host sends output directly to the console.
  • Write-Output sends 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-Host writes only to the console
  • Write-Output sends data to the pipeline
  • Write-Host is great for status messages and formatted text
  • Write-Output is better for returning data in functions or scripts
  • Write-Host returns $null; Write-Output returns the object
  • Use Write-Verbose or Write-Information for 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.