about_Redirection

업데이트 날짜: 2014년 5월

적용 대상: Windows PowerShell 2.0, Windows PowerShell 3.0, Windows PowerShell 4.0

항목

about_Redirection

간단한 설명

출력을 Windows PowerShell®에서 텍스트 파일로 리디렉션하는 방법을 설명합니다.

자세한 설명

기본적으로 Windows PowerShell는 명령 출력을 Windows PowerShell 콘솔에 보냅니다. 그러나 출력을 텍스트 파일로 지정하고 오류 출력을 일반 출력 스트림으로 리디렉션할 수 있습니다.

다음과 같은 방법을 사용하여 출력을 리디렉션할 수 있습니다.

  • - 명령 출력을 텍스트 파일로 보내는 Out-File cmdlet을 사용합니다. 일반적으로 Out-File cmdlet은 해당 매개 변수(예: Encoding, Force, Width 또는 NoClobber)를 사용해야 할 때 사용합니다.

  • - 명령 출력을 텍스트 파일로 보낸 다음 파이프라인에 보내는 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

참고: All(*), Warning(3), Verbose(4) 및 Debug(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>)는 파일의 현재 내용을 경고 없이 덮어씁니다. 그러나 파일이 읽기 전용, 숨겨진 또는 시스템 파일인 경우 리디렉션이 실패합니다. 추가 리디렉션 연산자(>> 및 >>)는 읽기 전용 파일에 쓰지 않고 내용을 시스템 또는 숨겨진 file.Insert 섹션 본문에 추가합니다.

내용을 읽기 전용, 숨겨진 또는 시스템 파일로 강제로 리디렉션하려면 Force 매개 변수를 지정하고 Out-File cmdlet을 사용합니다. 파일에 쓰는 경우 리디렉션 연산자는 Unicode 인코딩을 사용합니다. 파일의 인코딩이 다르면 출력이 올바르게 서식 설정되지 않습니다. 내용을 Unicode가 아닌 파일로 리디렉션하려면 Encoding 매개 변수를 지정하여 Out-File cmdlet을 사용합니다.

참고 항목

Out-File

Tee-Object

about_Operators

about_Command_Syntax

about_Path_Syntax