windows - variable - PowerShell difference between Write-Host and Write-Output?



powershell write-host variable (3)

What is the difference between Write-Host and Write-Output in PowerShell?

Like...

Write-Host "Hello World ";

Write-Output "Hello World";

https://src-bin.com


Answer #1

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.


Answer #2

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.


Answer #3

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.





powershell