首页 > 解决方案 > PowerShell 默认使用 Write-Host 还是 Write-Output

问题描述

当我"my text"自己在一行上输入时,文本会输出到控制台,但默认情况下是使用Write-Host还是Write-Output默认(我怎么能看到 - 我的意思是,我尝试使用| Get-Member来获得线索但不确定)?

我问的原因是我知道 Write-Host 在附近有管道命令的脚本中破坏顺序输出的已知问题。

标签: powershelltext

解决方案


所以,我做了一个小测试来解决这个问题。Out-Host 和 Write-Host,无论如何都不能重定向到任何流。这就是它们的工作方式,它们只是写入默认调用流。

使用这个逻辑,

"string1" | Out-host *>$null

将输出作为“string1”,因为它不能被重定向。但,

"string1" *>$null

将不显示任何输出,因此证明,写入任何文本与将其传递给 Write-Output cmdlet 相同:

# All are same
"string"
"string" | Write-Output
Write-Output "string"

推荐阅读