daniyalahmad
daniyalahmad

Reputation: 3843

PowerShell difference between Write-Host and Write-Output?

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

Like...

Write-Host "Hello World";

Write-Output "Hello World";

Upvotes: 193

Views: 186007

Answers (6)

SrJoven
SrJoven

Reputation: 206

One more thing about Write-Host vs Write-Output: inline String concatenation may not work as expected.

$sampleText = "World"    
Write-Host "Hello" $sampleText

returns

Hello World

but

$sampleText = "World"    
Write-Output "Hello" $sampleText

returns

Hello
World

This would encourage Write-Output with a variable (and use of concatenation) holding the entire string at once.

$hw = "Hello " + $sampleText
Write-Output $hw

Upvotes: 6

LAKSHAY ARORA
LAKSHAY ARORA

Reputation: 137

You can understand the difference between the two cmds with below example:

Write-host "msgtxt" | Get-Service

On running above, you will get output as "msgtxt"

Write-output "msgtxt" | Get-Service 

On running above, you will receive an error since msgtxt is not the name of any service.( In ideal condition) (Since you are writing it to a pipeline and it is being passed as input to Get-Service)

Upvotes: 2

Chad Carisch
Chad Carisch

Reputation: 2472

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.

Upvotes: 19

Summer
Summer

Reputation: 279

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.

Upvotes: 10

Shay Levy
Shay Levy

Reputation: 126742

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

Upvotes: 163

mjolinor
mjolinor

Reputation: 68273

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.

Upvotes: 65

Related Questions