Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell difference between Write-Host and Write-Output?

People also ask

What is Write-host in PowerShell?

Starting in Windows PowerShell 5.0, Write-Host is a wrapper for Write-Information This allows you to use Write-Host to emit output to the information stream. This enables the capture or suppression of data written using Write-Host while preserving backwards compatibility.

What is Write-output in PowerShell?

Write-Output sends objects to the primary pipeline, also known as the "output stream" or the "success pipeline." To send error objects to the error pipeline, use Write-Error . This cmdlet is typically used in scripts to display strings and other objects on the console.

Should I use Write-host?

It's always been recommended to avoid using Write-Host because it outputs only to the console and not to any of the standard output streams. As of PowerShell 5.0, Write-Host is just a wrapper for Write-Information and thus outputs to the standard output streams similar to the other Write-* cmdlets.

What is out host?

Description. The Out-Host cmdlet sends output to the PowerShell host for display. The host displays the output at the command line. Because Out-Host is the default, you don't have to specify it unless you want to use its parameters. Out-Host is automatically appended to every command that is executed.


In a nutshell, Write-Host writes to the console itself. Think of it as a MsgBox in VBScript. Write-Output, on the other hand, writes to the pipeline, so the next command can accept it as its input. You are not required to use Write-Output in order to write objects, as Write-Output is implicitly called for you.

PS> Get-Service

would be the same as:

PS> Get-Service | Write-Output

Write-Output sends the output to the pipeline. From there it can be piped to another cmdlet or assigned to a variable. Write-Host sends it directly to the console.

$a = 'Testing Write-OutPut'  | Write-Output
$b = 'Testing Write-Host' | Write-Host

Get-Variable a,b

Outputs:

Testing Write-Host

Name                           Value                                                                 
----                           -----                                                                 
a                              Testing Write-OutPut                                                  
b                                                  

If you don't tell Powershell what to do with the output to the pipeline by assigning it to a variable or piping it to anoher command, then it gets sent to out-default, which is normally the console so the end result appears the same.


Write-Output sends the data as an object through the pipeline. In the Questions example it will just pass a string.

write-host is host dependent. In the console write-host is essentially doing [console]::WriteLine. See this for more info.


Another difference between Write-Host and Write-Output:

  • Write-Host displays the message on the screen, but it does not write it to the log

  • Write-Output writes a message to the log, but it does not display it on the screen.

And Write-Host is considered as harmful. You can see a detailed explanation in Write-Host Considered Harmful.