Difference between Write-Output and Write-Host command in PowerShell?


Have we ever wondered, Write-Output and Write-Host both are used to print the strings or the output of the command then what is the difference between them?

Example

PS C:\> Write-Output "Test String"
Test String
PS C:\> Write-Host "Test String"
Test String

The output remains the same.

The first major difference is storing the output using the Pipeline structure. Write-Output and Write-Host both support the pipeline structure for example,

Example

"Test String" | Write-Output
Test String

"Test String" | Write-Host
Test String

The output remains the same.

The first major difference is storing the output using the Pipeline structure. Write-Output and Write-Host both support the pipeline structure for example,

Example

"Test String" | Write-Output
Test String

"Test String" | Write-Host
Test String

But when we store the output then it shows the difference between two commands.

Output

$a = "Test String" | Write-Output

Get-Variable a

Name Value
---- -----
a Test String


$b = "Test String" | Write-Host
Test String

Get-Variable b

Name Value
---- -----
b

In the above example, when the string output is stored into a variable, Write-Output can store it while Write-Host displays the output in the console and the output is not assigned to a variable because Write-Host produces output directly to the console while Write-Output produces and store the output inside the variable. That is the reason it is not recommended to use the Write-Host for the PowerShell remoting if scripter is not aware of the remote host having the console working properly or not.

Another difference is, with the Write-Host cmdlet, you can decorate the output with background and foreground (text) color and this is not possible in Write-Output command.

Example

Write-Host "Dark green backgound with White text" -BackgroundColor DarkGreen -ForegroundColor White

Dark green backgound with White text

Updated on: 11-Nov-2020

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements