Site icon Dotnet Helpers

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 us test this by the below cmdlet.

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

Example: 2

From Example 1, we think that it seemed to achieve the same result for both Write-Output and Write-Host but we have differences between these two cmdlets. This can be demonstrated in more detail in this example. 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 writes 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.

 

Exit mobile version