February 2019
Intermediate to advanced
626 pages
15h 51m
English
Output from a specific stream may be directed by placing the stream number on the left of the redirect operator.
For example, the output written by Write-Warning can be directed to a file:
PS> function Test-Redirect{>> 'This is standard out'>> Write-Warning 'This is a warning'>> }PS> $stdOut = Test-Redirect 3> 'warnings.txt'PS> Get-Content 'warnings.txt'This is a warning
When using the redirect operator, any file of the same name is overwritten. If information must be added to a file, the operator becomes >>:
$i = 1
function Test-Redirect{
Write-Warning "Warning $i"
}
Test-Redirect 3> 'warnings.txt' # Overwrite
$i++
Test-Redirect 3>> 'warnings.txt' # Append
It is possible to redirect additional streams, for example, ...
Read now
Unlock full access