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
