about_Redirection

Se aplica a: Windows PowerShell 2.0, Windows PowerShell 3.0, Windows PowerShell 4.0

TEMA

about_Redirection

DESCRIPCIÓN BREVE

Explica cómo redirigir la salida de Windows PowerShell® a archivos de texto.

DESCRIPCIÓN LARGA

De forma predeterminada, Windows PowerShell envía la salida del comando a la consola de Windows PowerShell. Sin embargo, puede dirigir la salida a un archivo de texto y puede redirigir la salida de error al flujo de salida normal.

Puede utilizar los métodos siguientes para redirigir la salida:

  • - Usar el cmdlet Out-File, que envía la salida del comando a un archivo de texto. Normalmente, se usa el cmdlet Out-File cuando se necesitan utilizar sus parámetros, como los parámetros Encoding, Force, Width o NoClobber.

  • - Usar el cmdlet Tee-Object, que envía la salida del comando a un archivo de texto y, después, la envía a la canalización.

  • - Usar los operadores de redirección de Windows PowerShell.

OPERADORES DE REDIRECCIÓN DE WINDOWS POWERSHELL

Los operadores de redirección le permiten enviar tipos de salida determinados a los archivos y al flujo de salida de procesos correctos.

Los operadores de redirección de Windows PowerShell utilizan los siguientes caracteres para representar cada tipo de salida:

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

NOTA: los operadores de redirección All (*), Warning (3), Verbose (4) y Debug (5) se introdujeron en Windows PowerShell 3.0. No funcionan en versiones anteriores de Windows PowerShell.

Los operadores de redirección de Windows PowerShell son los siguientes.

      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.     

La sintaxis de los operadores de redirección es la siguiente:

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

Si el archivo especificado ya existe, los operadores de redirección que no anexan datos (> y n>) sobrescriben el contenido actual del archivo sin ninguna advertencia previa. Sin embargo, si el archivo es de solo lectura, está oculto, o se trata de un archivo del sistema, la redirección producirá un error. Los operadores de redirección de anexión (>> y n>>) no escriben en un archivo de solo lectura, pero anexan contenido en un archivo oculto o de sistema.Insert section body here.

Para forzar la redirección del contenido a un archivo de solo lectura, oculto o del sistema, use el cmdlet Out-File con el parámetro Force. Cuando se escribe en archivos, los operadores de redirección utilizan codificación Unicode. Si el archivo tiene una codificación diferente, puede que el resultado no tenga un formato correcto. Para redirigir contenido a archivos que no sean Unicode, utilice el cmdlet Out-File con el parámetro Encoding.

VEA TAMBIÉN

Out-File

Tee-Object

about_Operators

about_Command_Syntax

about_Path_Syntax