PowerShell Write-Host vs Out-Host

One of my team members recently asked about the differences between Write-Host and Out-Host in PowerShell. As a PowerShell developer, you should know when to use which cmdlet. In this tutorial, I will explain everything about PowerShell Write-Host vs Out-Host with examples.

What is Write-Host in PowerShell?

Write-Host is a cmdlet in PowerShell used to display text directly to the console. It is typically used to display messages that do not need to be processed further in the pipeline.

Syntax

Here is the syntax:

Write-Host [-Object] <Object> [-NoNewline] [-Separator <Object>] [-ForegroundColor <ConsoleColor>] [-BackgroundColor <ConsoleColor>] [<CommonParameters>]

The primary purpose of Write-Host is to produce output for display only. This means it does not send output to the pipeline, which can be useful when you want to display information to the user without interfering with the data flow of your script.

Examples

Here is an example to help you understand it better.

# Display a simple message
Write-Host "Processing data for John Doe..."

# Display a message with colors
Write-Host "Error: Invalid input detected" -ForegroundColor Red -BackgroundColor Yellow

# Display multiple items with a separator
Write-Host "Alice" "Bob" "Charlie" -Separator ", "

In these examples, Write-Host is used to provide immediate feedback to the user. For instance, the first example informs the user that data for “John Doe” is being processed, while the second example highlights an error message in red text on a yellow background.

Here is the exact output in the screenshot below:

Differences between PowerShell Write-Host vs Out-Host

Check out PowerShell Write-Host vs Write-Verbose

What is Out-Host in PowerShell?

Out-Host is another cmdlet in PowerShell that sends output to the PowerShell host for display. Unlike Write-Host, Out-Host sends objects down the pipeline, allowing for further processing or display at the command line.

Syntax

Here is the syntax of Out-Host in PowerShell.

Out-Host [-Paging] [<CommonParameters>]

Out-Host is used to control how output is displayed in the console. It can be particularly useful when dealing with large amounts of data that need to be paginated or when you want to ensure that the output remains in the pipeline for further processing.

Examples

Here is an example.

# Send output to the host
Get-Process | Out-Host

# Paginate output
Get-Process | Out-Host -Paging

In these examples, Out-Host is used to display the list of running processes. The -Paging parameter is particularly useful when dealing with a large list, as it lets you view the output one page at a time.

Check out PowerShell Write-Host vs Write-Error

Key Differences Between Write-Host and Out-Host

Here is the summary of the differences between Write-Host and Out-Host, I’ve created the following table:

FeatureWrite-HostOut-Host
Output TypeDisplay-onlySends output to the pipeline
Use CaseDisplaying messages, debuggingDisplaying and paginating output
SyntaxWrite-Host "message"Get-Process | Out-Host
Color SupportYes (ForegroundColor, BackgroundColor)No
Pipeline InteractionNoYes

Conclusion

I explained the differences between Write-Host and Out-Host in PowerShell scripting in this tutorial. Write-Host is ideal for displaying messages directly to the console without affecting the pipeline. On the other hand, Out-Host is useful for sending output to the console while keeping it in the pipeline. If you still have any questions, feel free to comment below.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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