What is the difference between Write-Host and Write-Output??
I came across a useful article from PowerShell inventor about why you should not use Write-Host in general context unless you want to utilize specific features of it. It is a good read and worth having a peek. To output any data to console it is wiser to use Write-Output instead of Write-Host.
http://www.jsnover.com/blog/2013/12/07/write-host-considered-harmful/
The output returned by Write-Output is passed to pipeline so the functions/cmdlets after the pipeline can read this for further processing. This is not possible if you use Write-Host.
let us examine this behavior with a small example.
function Send-text { write-host "I am sending to console" } function Send-textwithwriteoutput { write-Output "I am sending to console" } function Receive-Text { process { write-Host "Received : $_" } }
I have three functions
- Send-Text #sends text to console using write-host
- Send-TextWithWriteOutput #sends text to console using write-output
- Receive-Text #receives input from pipeline and shows it
Now look at the below output:
It is very clear from the above output that when sent text to console using write-host, it is not passed to pipeline. But when write-output is used the receive-text function parsed and displayed the value.
Its not that you should not use Write-Host but what I mean is use at the right place. For example, no other cmdlet can replace Write-host cmdlet when you want to apply colors to text you are displaying to console. So, it is wiser to use write-output as a general practice as we expect our code to make the output utilized by other functions/cmdlets.
Hope it is clear. Happy learning.