I am having a little confusion about the various ways to print (echo) to the console. I have seen that there are multiple ways to write output to the console, such as:
我对打印(回显)到控制台的各种方法有点困惑。我已经看到有多种方法可以将输出写入控制台,例如:
Write-Host "Hello world1"
"Hello World2"
Out-Host -InputObject "Hello World3"
All three ways will print to the console. The middle one is somehow simpler and less verbose and easier to use. I also find that when you write a function such as:
这三种方式都将打印到控制台。中间的一个更简单,更简洁,更容易使用。我还发现当你编写一个函数时,例如:
function GetValues()
{
"1"
"2"
}
It still returns two strings in the pipeline:
它仍然在管道中返回两个字符串:
And I'm still able to print out the values:
我仍然可以打印出值:
foreach ($s in GetValues)
{
Write-Host "s: " $s
}
The thing that I have found was that using just the quoted string doesn't always appear on custom hosts, and that I have had to use Write-Host to get values to print on custom hosts.
我发现的事情是,仅使用引用的字符串并不总是出现在自定义主机上,而且我必须使用Write-Host来获取要在自定义主机上打印的值。
Somehow I find this confusing. Is "Print something"
supposed to be an alias to Write-Host
or what is the intent?
不知怎的,我发现这令人困惑。 “打印东西”应该是Write-Host的别名还是意图是什么?
2 个解决方案
#1
74
Default behaviour of PowerShell is just to dump everything that falls out of a pipeline without being picked up by another pipeline element or being assigned to a variable (or redirected) into Out-Host
. What Out-Host
does is obviously host-dependent.
PowerShell的默认行为只是转储掉管道外的所有内容,而不会被另一个管道元素拾取或分配给变量(或重定向)到Out-Host。 Out-Host的功能显然取决于主机。
Just letting things fall out of the pipeline is not a substitute for Write-Host
which exists for the sole reason of outputting text in the host application.
只是让事情脱离管道并不能代替Write-Host,它只是在主机应用程序中输出文本的唯一原因。
If you want output, then use the Write-*
cmdlets. If you want return values from a function, then just dump the objects there without any cmdlet.
如果需要输出,请使用Write- * cmdlet。如果要从函数返回值,则只需将对象转储到那里而不使用任何cmdlet。
#2
42
The middle one writes to the pipeline. Write-Host
and Out-Host
writes to the console. 'echo' is an alias for Write-Output
which writes to the pipeline as well. The best way to write to the console would be using the Write-Host
cmdlet.
中间的一个写入管道。 Write-Host和Out-Host写入控制台。 'echo'是Write-Output的别名,它也写入管道。写入控制台的最佳方法是使用Write-Host cmdlet。
When an object is written to the pipeline it can be consumed by other commands in the chain. For example:
当一个对象被写入管道时,它可以被链中的其他命令使用。例如:
"hello world" | Do-Something
but this won't work since Write-Host
writes to the console, not to the pipeline (Do-Something will not get the string):
但这不起作用,因为Write-Host写入控制台,而不是管道(Do-Something不会得到字符串):
Write-Host "hello world" | Do-Something
#1
74
Default behaviour of PowerShell is just to dump everything that falls out of a pipeline without being picked up by another pipeline element or being assigned to a variable (or redirected) into Out-Host
. What Out-Host
does is obviously host-dependent.
PowerShell的默认行为只是转储掉管道外的所有内容,而不会被另一个管道元素拾取或分配给变量(或重定向)到Out-Host。 Out-Host的功能显然取决于主机。
Just letting things fall out of the pipeline is not a substitute for Write-Host
which exists for the sole reason of outputting text in the host application.
只是让事情脱离管道并不能代替Write-Host,它只是在主机应用程序中输出文本的唯一原因。
If you want output, then use the Write-*
cmdlets. If you want return values from a function, then just dump the objects there without any cmdlet.
如果需要输出,请使用Write- * cmdlet。如果要从函数返回值,则只需将对象转储到那里而不使用任何cmdlet。
#2
42
The middle one writes to the pipeline. Write-Host
and Out-Host
writes to the console. 'echo' is an alias for Write-Output
which writes to the pipeline as well. The best way to write to the console would be using the Write-Host
cmdlet.
中间的一个写入管道。 Write-Host和Out-Host写入控制台。 'echo'是Write-Output的别名,它也写入管道。写入控制台的最佳方法是使用Write-Host cmdlet。
When an object is written to the pipeline it can be consumed by other commands in the chain. For example:
当一个对象被写入管道时,它可以被链中的其他命令使用。例如:
"hello world" | Do-Something
but this won't work since Write-Host
writes to the console, not to the pipeline (Do-Something will not get the string):
但这不起作用,因为Write-Host写入控制台,而不是管道(Do-Something不会得到字符串):
Write-Host "hello world" | Do-Something