PowerShell difference between Write-Host and 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.
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