Redirection Operators
The Redirection operators are used in PowerShell to redirect the output from the PowerShell console to text files.
The following table shows the numbers which are used by the PowerShell redirection operators to represent the available output streams:
Stream | Description | Introduced in |
---|---|---|
1 | Success Stream | PowerShell 2.0 |
2 | Error Stream | PowerShell 2.0 |
3 | Warning Stream | PowerShell 3.0 |
4 | Verbose Stream | PowerShell 3.0 |
5 | Debug Stream | PowerShell 3.0 |
6 | Information Stream | PowerShell 5.0 |
* | All Streams | PowerShell 3.0 |
PowerShell supports the following redirection operators:
- >
- >>
- >&1
>Operator
This operator is used to send the specified stream to the specified text file. The following statement is the syntax to use this operator:
Example:
The above command sends the following output of the Get-childItem cmdlet to the k.txt file.
Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 30-09-2019 05:49 images d----- 09-10-2019 11:14 files d----- 09-10-2019 11:56 powershell d----- 09-10-2019 10:58 users d----- 09-10-2019 04:37 Windows -a---- 05-11-2019 14:53 0 k.txt -a---- 25-06-2018 09:46 2179 powershell.txt
>> Operator
This operator is used to append the specified stream to the specified text file. The following statement is the syntax to use this operator:
Example:
The above command appends the output of the get-help command to the k.txt file.
>&1 Operator
This operator is used to redirect the specified stream to the success stream. The following statement is the syntax to use this operator:
Example:
The above command is used to redirect the following output of Write-Error command to the k.txt file.
Write-Error "hello" : hello At line:1 char:1+ &{Write-Error "hello" }2>&1 > D:k.txt + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException