共用方式為


關於重新導向

簡短描述

說明如何將PowerShell的輸出重新導向至文字檔。

完整描述

根據預設,PowerShell 會將輸出傳送至 PowerShell 主機。 這通常是主控台應用程式。 不過,您可以將輸出導向文本檔,並將錯誤輸出重新導向至一般輸出數據流。

您可以使用下列方法來重新導向輸出:

  • Out-File使用 Cmdlet,它會將命令輸出傳送至文字檔。 一般而言,當您 Out-File 需要使用 Cmdlet 的參數時,例如 EncodingForceWidthNoClobber 參數。

  • Tee-Object使用 Cmdlet,它會將命令輸出傳送至文本文件,然後將它傳送至管線。

  • 使用 PowerShell 重新導向運算子。 使用重新導向運算元搭配檔案目標的功能相當於沒有額外參數的管線 Out-File

如需數據流的詳細資訊,請參閱 about_Output_Streams

可重新導向的輸出數據流

PowerShell 支援重新導向下列輸出數據流。

Stream# Description 在中引進 寫入 Cmdlet
1 成功 Stream PowerShell 2.0 Write-Output
2 錯誤 Stream PowerShell 2.0 Write-Error
3 警告 Stream PowerShell 3.0 Write-Warning
4 詳細資訊 Stream PowerShell 3.0 Write-Verbose
5 錯 Stream PowerShell 3.0 Write-Debug
6 資訊 Stream PowerShell 5.0 Write-Information
* 所有數據流 PowerShell 3.0

注意

PowerShell 也有 進度 數據流,但不支援重新導向。

PowerShell 重新導向運算符

PowerShell 重新導向運算符如下所示,其中 n 代表數據流編號。 如果未指定 任何數據流, 則成功數據流 ( 1 ) 為預設值。

運算子 描述 語法
> 將指定的數據流傳送至檔案。 n>
>> 指定的數據流附加至檔案。 n>>
>&1 指定的數據流重新導向至 成功 數據流。 n>&1

注意

不同於某些 Unix 殼層,您只能將其他數據流重新導向至 成功 數據流。

範例

範例 1:將錯誤和輸出重新導向至檔案

這個範例會在一個將會成功的專案上執行 dir ,另一個專案將會發生錯誤。

dir 'C:\', 'fakepath' 2>&1 > .\dir.log

它會使用 2>&1錯誤 數據流重新導向至 成功 數據流,並將 > 產生的 成功 數據流傳送至名為的檔案 dir.log

範例 2:將所有成功數據流數據傳送至檔案

本範例會將所有 Success 數據流數據傳送至名為 的 script.log檔案。

.\script.ps1 > script.log

範例 3:將成功、警告和錯誤數據流傳送至檔案

此範例示範如何結合重新導向運算符以達到所需的結果。

&{
   Write-Warning "hello"
   Write-Error "hello"
   Write-Output "hi"
} 3>&1 2>&1 > P:\Temp\redirection.log
  • 3>&1 會將 警告 數據流重新導向至 成功 數據流。
  • 2>&1錯誤 數據流重新導向至 成功 數據流 (,現在也會包含所有 警告 數據流數據)
  • > 會將 成功 數據流重新導向 (,此數據流現在同時包含 警告錯誤 數據流,) 名為 C:\temp\redirection.log)

範例 4:將所有數據流重新導向至檔案

此範例會將呼叫之腳本 script.ps1 的所有數據流輸出傳送至名為的檔案 script.log

.\script.ps1 *> script.log

範例 5:隱藏所有 Write-Host 和資訊數據流數據

本範例會隱藏所有資訊數據流數據。 若要深入瞭解 信息 數據流 Cmdlet,請參閱 Write-HostWrite-Information

&{
   Write-Host "Hello"
   Write-Information "Hello" -InformationAction Continue
} 6> $null

範例 6:顯示動作喜好設定的效果

動作喜好設定變數和參數可以變更寫入至特定數據流的內容。 此範例中的腳本顯示的值 $ErrorActionPreference 如何影響寫入 至 Error 數據流的內容。

$ErrorActionPreference = 'Continue'
$ErrorActionPreference > log.txt
get-item /not-here 2>&1 >> log.txt

$ErrorActionPreference = 'SilentlyContinue'
$ErrorActionPreference >> log.txt
get-item /not-here 2>&1 >> log.txt

$ErrorActionPreference = 'Stop'
$ErrorActionPreference >> log.txt
Try {
    get-item /not-here 2>&1 >> log.txt
}
catch {
    "`tError caught!" >> log.txt
}
$ErrorActionPreference = 'Ignore'
$ErrorActionPreference >> log.txt
get-item /not-here 2>&1 >> log.txt

$ErrorActionPreference = 'Inquire'
$ErrorActionPreference >> log.txt
get-item /not-here 2>&1 >> log.txt

$ErrorActionPreference = 'Continue'

當我們執行此腳本時,會在 設定為 Inquire$ErrorActionPreference收到提示。

PS C:\temp> .\test.ps1

Confirm
Cannot find path 'C:\not-here' because it does not exist.
[Y] Yes  [A] Yes to All  [H] Halt Command  [S] Suspend  [?] Help (default is "Y"): H
Get-Item: C:\temp\test.ps1:23
Line |
  23 |  get-item /not-here 2>&1 >> log.txt
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | The running command stopped because the user selected the Stop option.

當我們檢查記錄檔時,會看到下列內容:

PS C:\temp> Get-Content .\log.txt
Continue

Get-Item: C:\temp\test.ps1:3
Line |
   3 |  get-item /not-here 2>&1 >> log.txt
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot find path 'C:\not-here' because it does not exist.

SilentlyContinue
Stop
    Error caught!
Ignore
Inquire

備註

未附加數據 (>n>) 覆寫指定檔案目前內容的重新導向運算符,而不發出警告。

不過,如果檔案是唯讀、隱藏或系統檔案,則重新導向 會失敗。 附加重新導向運算子 (>>n>>) 不會寫入唯讀檔案,但會將內容附加至系統或隱藏的檔案。

若要強制將內容重新導向至只讀、隱藏或系統檔案,請使用 Out-File Cmdlet 搭配其 Force 參數。

當您寫入檔案時,重新導向運算子會使用 UTF8NoBOM 編碼。 如果檔案有不同的編碼方式,則輸出可能無法正確格式化。 若要以不同的編碼方式寫入檔案,請使用 Out-File Cmdlet 搭配其 Encoding 參數。

比較運算子的潛在混淆

>運算子不會與大於比較運算元混淆, (通常以其他程式設計語言) 表示>

視所比較的物件而定,使用 > 的輸出看起來可能正確 (,因為 36 不大於 42) 。

PS> if (36 > 42) { "true" } else { "false" }
false

不過,本機文件系統的檢查可以看到已寫入名為 42 的檔案,其中包含 內容 36

PS> dir

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
------          1/02/20  10:10 am              3 42

PS> cat 42
36

嘗試使用反向比較 < (小於) ,會產生系統錯誤:

PS> if (36 < 42) { "true" } else { "false" }
At line:1 char:8
+ if (36 < 42) { "true" } else { "false" }
+        ~
The '<' operator is reserved for future use.
+ CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : RedirectionNotSupported

如果數值比較是必要的運算, -lt-gt 應該使用 。 請參閱: -gt 比較運算符

另請參閱