PowerShell 中的 Echo 等效命令
Rohan Timalsina
2023年1月30日
PowerShell
PowerShell Echo
-
在 PowerShell 中使用
Write-Output作为echo等效命令 -
在 PowerShell 中使用
Write-Host作为echo等效命令 -
在 PowerShell 中使用
Write-Debug作为 Echo 等效命令 -
在 PowerShell 中使用
Write-Verbose作为 Echo 等效命令
echo 命令在控制台上打印字符串或变量。本教程将介绍与 PowerShell 脚本和函数中的 echo 等效的不同命令。
echo "Hello World."
输出:
Hello World.
在 PowerShell 中使用 Write-Output 作为 echo 等效命令
最接近的 echo 等效命令是 Write-Output。echo 是 Write-Output 的内置别名。Write-Output 将输出数据写入 pipeline 并允许你将输出重定向到另一个命令或文件。如果管道未完成,输出数据将显示在 PowerShell 控制台上。
Write-Output "Hello World."
输出:
Hello World.
在 PowerShell 中使用 Write-Host 作为 echo 等效命令
PowerShell 中的 Write-Host cmdlet 用于将输出直接写入 PowerShell 控制台。你还可以使用 -ForegroundColor 和 -BackgroundColor 参数自定义字体颜色和背景颜色。
Write-Host "Hello World."
输出:
Hello World.
在 PowerShell 中使用 Write-Debug 作为 Echo 等效命令
如果 $DebugPreference 设置为 Continue 或 Stop,Write-Debug cmdlet 会将调试消息直接写入 PowerShell 控制台。 $DebugPreference 的默认值是 SilentlyContinue。
$DebugPreference = Continue
Write-Debug "Hello World."
输出:
DEBUG: Hello World.
在 PowerShell 中使用 Write-Verbose 作为 Echo 等效命令
如果 $VerbosePreference 设置为 Continue 或 Stop,Write-Verbose cmdlet 会将详细消息直接写入 PowerShell 控制台。
$VerbosePreference = Continue
Write-Verbose "Hello World."
输出:
DEBUG: Hello World.
Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
作者: Rohan Timalsina
