about_Redirection

適用於: Windows PowerShell 2.0, Windows PowerShell 3.0, Windows PowerShell 4.0, Windows PowerShell 5.0

主題

about_Redirection

簡短描述

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

詳細描述

根據預設,Windows PowerShell 會將其命令輸出傳送至 Windows PowerShell 主控台。不過,您可以將輸出導向至文字檔,而且可以將錯誤輸出重新導向至一般輸出資料流。

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

  • - 使用 Out-File Cmdlet,將命令輸出傳送至文字檔。當您需要使用其參數 (如 Encoding、Force、Width 或 NoClobber 參數) 時,您通常會使用 Out-File Cmdlet。

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

  • - 使用 Windows PowerShell 重新導向運算子。

WINDOWS POWERSHELL 重新導向運算子

重新導向運算子可讓您將特定類型的輸出傳送至檔案和成功輸出資料流。

Windows PowerShell 重新導向運算子會使用下列字元來表示每個輸出類型:

        *   All output
        1   Success output
        2   Errors
        3   Warning messages
        4   Verbose output
        5   Debug messages

注意:Windows PowerShell 3.0 引進了所有 (*)、警告 (3)、詳細資訊 (4) 和偵錯 (5) 重新導向運算子。它們不適用於舊版的 Windows PowerShell。

Windows PowerShell 重新導向運算子如下所示。

      Operator  Description                Example  
      --------  ----------------------     ------------------------------
      >         Sends output to the        Get-Process > Process.txt
                specified file.

      >>        Appends the output to      dir *.ps1 >> Scripts.txt
                the contents of the  
                specified file.

      2>        Sends errors to the        Get-Process none 2> Errors.txt
                specified file.
 
      2>>       Appends errors to          Get-Process none 2>> Save-Errors.txt
                the contents of the 
                specified file.
 
      2>&1      Sends errors (2) and       Get-Process none, Powershell 2>&1
                success output (1) 
                to the success 
                output stream.

      3>        Sends warnings to the      Write-Warning "Test!" 3> Warnings.txt
                specified file.

      3>>       Appends warnings to        Write-Warning "Test!" 3>> Save-Warnings.txt
                the contents of the 
                specified file.

      3>&1      Sends warnings (3) and     function Test-Warning 
                success output (1)         {  Get-Process PowerShell; 
                to the success                Write-Warning "Test!" }
                output stream.             Test-Warning 3>&1

      4>        Sends verbose output to    Import-Module * -Verbose 4> Verbose.txt
                the specified file.

      4>>       Appends verbose output     Import-Module * -Verbose 4>> Save-Verbose.txt
                to the contents of the 
                specified file.

      4>&1      Sends verbose output (4)   Import-Module * -Verbose 4>&1
                and success output (1)    
                to the success output
                stream.              
 
      5>        Sends debug messages to    Write-Debug "Starting" 5> Debug.txt
                the specified file.

      5>>       Appends debug messages     Write-Debug "Saving" 5>> Save-Debug.txt
                to the contents of the 
                specified file.

      5>&1      Sends debug messages (5)   function Test-Debug 
                and success output (1)     { Get-Process PowerShell 
                to the success output        Write-Debug "PS" }
                stream.                    Test-Debug 5>&1

      *>        Sends all output types     function Test-Output
                to the specified file.     { Get-Process PowerShell, none  
                                             Write-Warning "Test!"
      *>>       Appends all output types     Write-Verbose "Test Verbose"
                to the contents of the       Write-Debug "Test Debug" } 
                specified file.            
                                           Test-Output *> Test-Output.txt
      *>&1      Sends all output types     Test-Output *>> Test-Output.txt
                (*) to the success output  Test-Output *>&1      
                stream.     

重新導向運算子的語法如下所示:

       <input> <operator> [<path>\]<file>

如果指定的檔案已經存在,則不會附加資料的重新導向運算子 (> 和 n>) 會覆寫檔案的目前內容,但不會顯示警告。不過,如果檔案是唯讀、隱藏或系統檔案,則重新導向會失敗。附加重新導向運算子 (>> 和 n>>) 不會寫入唯讀檔案,但它們會將內容附加到系統或隱藏檔案。在此插入區段內文。

若要強制將內容重新導向至唯讀、隱藏或系統檔案,請使用 Out-File Cmdlet 搭配其 Force 參數。當您寫入檔案時,重新導向運算子會使用 Unicode 編碼方式。如果檔案有不同的編碼方式,則輸出的格式可能不正確。若要將內容重新導向至非 Unicode 檔案,請使用 Out-File Cmdlet 搭配其 Encoding 參數。

另請參閱

Out-File

Tee-Object

about_Operators

about_Command_Syntax

about_Path_Syntax