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 コマンドレットを使用する。このコマンドレットによって、コマンドの出力がテキスト ファイルに送信されます。通常、Encoding、Force、Width、NoClobber などのパラメーターを使用する必要がある場合は、Out-File コマンドレットを使用します。

  • - Tee-Object コマンドレットを使用する。このコマンドレットによって、コマンドの出力がテキスト ファイルに送信され、その後パイプラインに送信されます。

  • - Windows PowerShell のリダイレクト演算子を使用する。

Windows PowerShell のリダイレクト演算子

リダイレクト演算子を使用すると、特定の種類の出力をファイルと正常出力ストリームに送信できます。

Windows PowerShell のリダイレクト演算子では、次の文字を使用して出力の種類を表します。

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

注記:すべて (*)、警告 (3)、詳細 (4)、デバッグ (5) の各リダイレクト演算子は、Windows PowerShell 3.0 で導入されました。これらは、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 コマンドレットと Force パラメーターを使用します。リダイレクト演算子では、ファイルに書き込むときに Unicode エンコードが使用されます。ファイルで別のエンコードが指定されている場合は、正常な形式で出力されない可能性があります。内容を Unicode 以外のファイルにリダイレクトするには、Out-File コマンドレットと Encoding パラメーターを使用します。

関連項目

Out-File

Tee-Object

about_Operators

about_Command_Syntax

about_Path_Syntax