PowerShell Write-Host vs Write-Output

As a PowerShell developer or admin, you should know the differences between Write-Host and Write-Output. In this tutorial, I will explain everything about PowerShell Write-Host vs Write-Output with examples.

What is Write-Host in PowerShell?

The Write-Host cmdlet is used to display text directly to the console. It is used to produce output that is meant for human eyes rather than to pass data along a pipeline or store it in variables.

This makes Write-Host ideal for creating user prompts or for displaying messages that don’t need to be processed further.

For example, let’s say I want to greet a user by name:

$UserName = "John Doe"
Write-Host "Welcome, $UserName!"

In this case, Write-Host will display “Welcome, John Doe!” directly on the console. This output is not captured or stored; it’s purely for the user’s benefit.

Here is the exact output in the screenshot below:

PowerShell Write-Host vs Write-Output

Here is an example of using Write-Host:

$UserName = "Jane Smith"
Write-Host "Hello, $UserName! Welcome to the PowerShell tutorial."

This will display: Hello, Jane Smith! Welcome to the PowerShell tutorial. directly on the console.

Check out PowerShell: Where-Object vs Select-Object

What is Write-Output in PowerShell?

On the other hand, Write-Output PowerShell cmdlet sends data to the output stream, making it available for further processing. This cmdlet is essential for passing data between different parts of a script or function.

Unlike Write-Host, the output from Write-Output can be assigned to variables, piped to other cmdlets, or redirected to files.

Consider the following example where we generate a list of cities:

$Cities = "New York", "Los Angeles", "Chicago"
Write-Output $Cities

Here, Write-Output sends the list of cities to the output stream. This data can be captured in a variable or piped to another cmdlet for further processing, such as sorting:

$Cities = "New York", "Los Angeles", "Chicago"
$SortedCities = Write-Output $Cities | Sort-Object

Here is the output in the screenshot below:

Write-Host vs Write-Output in PowerShell

Here is another example of using Write-Output in PowerShell.

$States = "California", "Texas", "Florida"
$OutputStates = Write-Output $States
$OutputStates | ForEach-Object { Write-Host "Processing state: $_" }

This script sends the list of states to the output stream and then processes each state, displaying a message for each one.

Check out PowerShell ForEach-Object vs ForEach

PowerShell Write-Host vs Write-Output : Key Differences

To summarize, the key differences between Write-Host and Write-Output in PowerShell are:

  • Purpose: Write-Host is for displaying messages directly to the console, while Write-Output is for sending data to the output stream.
  • Usage: Use Write-Host for user prompts and messages that don’t need further processing. Use Write-Output for data that needs to be captured, piped, or processed further.
  • Output Handling: Write-Host does not send data to the pipeline, whereas Write-Output does.

Here is the summary in tabular format.

FeatureWrite-HostWrite-Output
Primary PurposeDisplay messages directly to the consoleSend data to the output stream
Usage ScenarioUser prompts, informational messagesData processing, passing data between cmdlets
Output HandlingNo pipeline outputSends data to the pipeline
Example Use CaseWrite-Host "Hello, John!"$Data = Write-Output "New York", "Los Angeles"

Conclusion

I hope now you understand when to use Write-Host versus Write-Output in PowerShell. In this tutorial, I explained the difference between PowerShell Write-Host vs Write-Output with examples.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.