Redirecting streams to standard output

Streams can be redirected to standard output in PowerShell. The destination stream is written on the right-hand side of the redirect operator (without a space). Stream numbers on the right-hand side are prefixed with an ampersand (&) to distinguish the stream from a filename.

For example, the Information output written by the following command is sent to standard output:

PS> function Test-Redirect{
'This is standard out'

Write-Information 'This is information'
}

$stdOut = Test-Redirect 6>&1
$stdOut

This is standard out
This is information

It is possible to redirect additional streams, for example, warnings and errors, by adding more redirect statements. The following example redirects the error and warning streams to standard output:

function Test-Redirect{ 
    'This is standard out' 
    
    Write-Error 'This is an error' 
    Write-Warning 'This is a warning' 
} 
$stdOut = Test-Redirect 2>&1 3>&1 

The wildcard character * may be used to represent all streams if all streams were to be sent to another:

$verbosePreference = 'continue' 
function Test-Redirect{ 
    'This is standard out' 
 
    Write-Information 'This is information' 
    Write-Host 'This is information as well' 
    Write-Error 'This is an error' 
    Write-Verbose 'This is verbose' 
    Write-Warning 'This is a warning' 
} 
$stdOut = Test-Redirect *>&1 

The preceding example starts by setting the verbose preference variable. Without this the output from Write-Verbose will not be shown at all.

Only StdOut:
Each of the preceding examples shows redirection to StdOut. It is not possible to redirect to streams other than standard output.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset