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

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

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

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

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

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

    如需資料流程的詳細資訊,請參閱 about_Output_Streams

    可重新導向的輸出資料流程

    PowerShell 支援重新導向下列輸出資料流程。

    寫入 Cmdlet

    成功 錯誤 資料流程類似于其他殼層的 stdout 和 stderr 資料流程。 不過,stdin 未連線到 PowerShell 管線進行輸入。

    PowerShell 重新導向運算子

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

    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 > C:\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
        Error caught!
    Ignore
    Inquire
    

    不會附加資料 (>n>) 覆寫指定檔案目前內容的重新導向運算子,而不會發出警告。

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

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

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

    寫入檔案時的輸出寬度

    當您使用 Out-File 或 重新導向運算子寫入檔案時,PowerShell 會根據檔案執行所在的主控台寬度,將資料表輸出格式化為檔案。 例如,在主控台寬度設定為 80 個數據行的系統上,使用之類的 Get-ChildItem Env:\Path > path.log 命令將資料表輸出記錄到檔案時,檔案中的輸出會截斷為 80 個字元:

    Name                         Value
    ----                         -----
    Path                         C:\Program Files\PowerShell\7;C:\WINDOWS…
    

    假設您可以在執行腳本的系統上任意設定主控台寬度,您可能會偏好根據您指定的寬度,將 PowerShell 格式資料表輸出設定為檔案。

    Out-FileCmdlet 提供Width參數,可讓您設定資料表輸出所需的寬度。 您可以使用 變數來設定腳本中 Cmdlet 的所有使用方式 Out-File ,而不需在叫用 $PSDefaultParameterValues 的任何位置 Out-File 新增 -Width 2000 此值。 而且,由於重新導向運算子 (>>>) 實際上是 的 Out-File 別名,因此設定 Out-File:Width 整個腳本的參數也會影響重新導向運算子的格式化寬度。 將下列命令放在腳本頂端附近,以針對整個腳本設定 Out-File:Width

    $PSDefaultParameterValues['out-file:width'] = 2000
    

    當記錄資料表格式化輸出時,增加輸出寬度會增加記憶體耗用量。 如果您要將許多表格式資料記錄到檔案,而且您知道可以透過較小的寬度取得,請使用較小的寬度。

    在某些情況下,例如 Get-Service 輸出,若要使用額外的寬度,您必須在輸出至檔案之前透過管線傳送輸出 Format-Table -AutoSize

    $PSDefaultParameterValues['out-file:width'] = 2000
    Get-Service | Format-Table -AutoSize > services.log
    

    如需 的詳細資訊 $PSDefaultParameterValues ,請參閱 about_Preference_Variables

    比較運算子的潛在混淆

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

    視要比較的物件而定,使用 > 的輸出看起來可能是正確的 (,因為 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
    

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

    PS> if (36 < 42) { "true" } else { "false" }
    ParserError:
    Line |
       1 |  if (36 < 42) { "true" } else { "false" }
         |         ~
         | The '<' operator is reserved for future use.
    

    如果數值比較是必要作業, -lt-gt 應該使用 。 如需詳細資訊,請參閱 -gtabout_Comparison_Operators中的 運算子。

  • about_Command_Syntax
  • about_Operators
  • about_Output_Streams
  • about_Path_Syntax
  • Out-File
  • Tee-Object
  • Write-Debug
  • Write-Error
  • Write-Host
  • Write-Information
  • Write-Output
  • Write-Progress
  • Write-Verbose
  • Write-Warning
  •