PowerShell Write-Host vs Write-Information

Today, at a PowerShell discussion, someone asked about the difference between PowerShell Write-Host Vs Write-Information. PowerShell developers need to know about it. In this tutorial, I will explain the details.

PowerShell Write-Host

Write-Host is mainly used to display messages directly to the console. It is ideal for showing output that is meant for the user to read, such as status messages or prompts.

However, it’s important to note that Write-Host does not send output to the pipeline, which means the data it outputs cannot be used by other cmdlets.

Syntax

Here is the syntax of Write-Host.

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

Example

Let me show you an example to help you understand better.

Suppose you want to check the status of servers in different states. We want to display a message indicating the server’s status.

$servers = @("California-Server1", "Texas-Server2", "NewYork-Server3")

foreach ($server in $servers) {
    # Simulate checking server status
    $status = "Online"
    Write-Host "$server is $status" -ForegroundColor Green
}

In this example, Write-Host is used to display the server status in green text directly to the console.

Here is the exact output in the screenshot below:

write-host vs write-information in PowerShell

Check out PowerShell Write-Host vs Write-Error

PowerShell Write-Information

Write-Information was introduced in PowerShell 5.0 and is used to write informational messages to the information stream. This cmdlet allows you to specify how PowerShell handles information stream data for a command, making it more useful than Write-Host. The information stream can be redirected or suppressed, which provides more control over the output.

Syntax

Here is the syntax of Write-Information.

Write-Information [-MessageData] <Object> [-Tags <Object[]>] [-NoNewline] [-InformationAction <ActionPreference>] [-InformationVariable <String>] [<CommonParameters>]

Example

Continuing with our server status script, let’s use Write-Information to log the status messages.

$servers = @("California-Server1", "Texas-Server2", "NewYork-Server3")

foreach ($server in $servers) {
    # Simulate checking server status
    $status = "Online"
    Write-Information "$server is $status"
}

In this example, Write-Information logs the server status to the information stream, which can be redirected or handled differently based on the script’s needs.

Read PowerShell Write-Host vs Echo

Key Differences Between Write-Host and Write-Information

Here are the key differences between Write-Host and Write-Information in PowerShell.

FeatureWrite-HostWrite-Information
Output DestinationDirectly to the consoleInformation stream
Pipeline InteractionDoes not send output to the pipelineSends output to the information stream, which can be redirected or suppressed
CustomizationSupports text color customization with -ForegroundColor and -BackgroundColor parametersSupports tagging with -Tags parameter and handling with -InformationAction and -InformationVariable parameters
Use CaseBest for displaying messages to the user directlyBest for logging informational messages that might need to be redirected or handled programmatically

Conclusion

In this tutorial, I explained the difference between Write-Host and Write-Information in PowerShell. You must know now when to use Write-Host and when to use Write-Information.

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.