windows 写入主机和写入输出之间的 PowerShell 区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19754069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PowerShell difference between Write-Host and Write-Output?
提问by daniyalahmad
What is the difference between Write-Hostand Write-Outputin PowerShell?
PowerShell 中的Write-Host和Write-Output 有什么区别?
Like...
喜欢...
Write-Host "Hello World ";
Write-Output "Hello World";
采纳答案by Shay Levy
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.
简而言之,Write-Host
写入控制台本身。将其视为VBScript 中的 MsgBox 。Write-Output
,另一方面,写入管道,因此下一个命令可以接受它作为其输入。您不需要使用Write-Output
来编写对象,因为它Write-Output
是隐式调用的。
PS> Get-Service
would be the same as:
将与:
PS> Get-Service | Write-Output
回答by mjolinor
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.
Write-Output 将输出发送到管道。从那里它可以通过管道传输到另一个 cmdlet 或分配给一个变量。Write-Host 将其直接发送到控制台。
$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 如何处理管道的输出,那么它会被发送到 out-default,这通常是控制台,因此最终结果看起来相同.
回答by Chad Carisch
Write-Output
sends the data as an object through the pipeline. In the Questions example it will just pass a string.
Write-Output
通过管道将数据作为对象发送。在问题示例中,它只会传递一个字符串。
write-host
is host dependent. In the console write-host
is essentially doing [console]::WriteLine
.
See thisfor more info.
write-host
是主机依赖的。在控制台write-host
本质上是在做[console]::WriteLine
。有关更多信息,请参阅此内容。
回答by Summer
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.
Write-Host 将消息显示在屏幕上,但不会将其写入日志
Write-Output 将消息写入日志,但不会在屏幕上显示。
And Write-Host is considered as harmful. You can see a detailed explanation in Write-Host Considered Harmful.
并且写主机被认为是有害的。你可以在写主机被认为有害中看到详细的解释。