Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

powershell - What is the difference in result by write-host and write-output?

I am new to PowerShell and while doing some code I came through this thing. While I use Write-Host and Write-Output to perform the same query, I get different results:

PS> Write-Output $PSVersionTable                                                                                                                   

Name                           Value                                                                                                                                               
----                           -----                                                                                                                                               
PSVersion                      5.0.10586.117                                                                                                                                          
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                             
BuildVersion                   10.0.10586.117
CLRVersion                     4.0.30319.17929
WSManStackVersion              3.0                                                                                                                                                 
PSRemotingProtocolVersion      2.3                                                                                                                                                 
SerializationVersion           1.1.0.1                                                                                                                                             

vs.

PS> Write-Host $PSVersionTable                                                                                                                   
System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Important difference:

Write-output writes an object to the pipeline, so the next command can accept it as it's input. While Write-host just write directly to the console.

In your example

Write-Output will write the object to the pipeline, and Out-Default (Hidden at the end of the pipeline) will display the object values in either a table or a list.

Write-Host writes directly to the console instead of using the default cmdlet Out-default in the end of the pipeline. In other words, Write-Host does not write anything to the pipeline, only displays what he sees to the console.. This is why you can add parameters like -foregroundcolor to the Write-Host cmdlet, but not to the Write-output cmdlet.

Write-Host is useful when debugging and you need to display text in diffrent colors.

You can simply test this with

Write-Output "Hello World" | Get-Member

We can se that this is a System.String Object.

If we run:

Write-Host "Hello World" | Get-Member

We get an error: gm : You must specify an object for the Get-Member cmdlet ...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...