Home/PowerShell, Powershell demo/Difference between write-host and write-output in powershell

Difference between write-host and write-output in powershell

One of the great benefits of PowerShell is its pipeline mechanics that enable the output of one command to be passed to the next command in the pipeline. That output is not sent as basic text but rather the actual objects generated from a command are sent in the native object format to the next command and this is enabled for all commands.

Example: 1

If you open a PowerShell window and type the following commands the result is the same for both, Let we test this by below cmdlet.

If we looks the above script, it like they are the same but in reality they are working in very different ways. Let we discuss more to understand this.

Example: 2

From Example 1, we think that it seemed to achieve the same result for both Write-Output and Write-Host but we having difference among this two cmdlet. This can be demonstrated with a more detailed in this exampe. 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

From the above example, we can able see the Write-Output cmdlet has passed the value to the next process but Write-Host just directly write to the host and nothing has to send forward to the next cmdlet. 

In simpler, if you possibly want to write data to the screen, but also want the data to be passed down the pipeline to further commands, then use Write-Output.  Write-Host does not output data to PowerShell Object flow Engine but rather, as the name implies, writes directly to the host and sends nothing to the PowerShell engine to be forwarded to commands later in the pipeline.

 

One Comment

  1. Writershelp May 13, 2018 at 2:02 am

    Thanks, great information!

Leave a Reply