PowerShell Redirection Operators
In this tutorial we will go through PowerShell Redirection Operators. PowerShell by default sends the output of the commands, warnings and errors in the console. You are able to send those outputs to files in order to store them. There are different ways to redirect the output to files:
- Redirection Operators
We will not go though the commands in this tutorial. We will only check the redirection operators.
PowerShell redirection operators are using specific characters to specify the output to files Please check the list below:
*– All output
1– Success output
3– Warnings messages
4– Verbose Output
5– Debug messages
6– Informational messages
In order to use
5 you need to have PowerShell 3.0 or above. Those four types were introduced in Powershell 3.0. The newer type that has been introduced in PowerShell 5.0 is
6. You need to have PowerShell 5.0 or above in order to used it.
Now we will go through the list of the redirection operators and see some examples.
>– Sends output to the specified file
>>– Appends output to the content of the specified file
2>– Sends errors to the specified file.
2>>– Appends errors to the content of the specified file
2>&1– Sends errors and success output to the success output stream
3>– Sends warnings to the specified file
3>>– Appends warnings to the contents of the specified file.
3>&1– Sends warnings and success output to the success output stream
4>– Sends verbose output to the specified file
4>>– Appends verbose output to the contents of the specified file
4>&1– Sends verbose output and success output to the success output stream
5>– Sends debug messages to the specified file
5>>– Appends debug messages to the contents of the specified file
5>&1– Sends debug messages and success output to the success output stream
6>– Sends informational messages to a specified file
6>>– Appends informational messages to the contents of a specified file
6>&1– Sends informational messages and success output to the success output stream.
*>– Sends all output types to the specified file
*>>– Appends all output types to the contents of the specified file
*>&1– Sends all output types to the success output stream
Get-NetAdapter > C:\Scripts_Output\NetAdapters.txt
After you will run the command above, the output of the command will be redirected to the file specified. Below is the content of the file.
Then I run the next command and all the content as you can see at the below output, has been replaced.
Get-NetAdapter -IncludeHidden > C:\Scripts_Output\NetAdapters.txt
In this example, we will use the same command and redirect the out put to the same file. The difference in this example is that I have changed the redirection operator. I am using
>> as redirection operator and the result is to append the second command to the content of the file and not replace it as example 1.
Get-NetAdapter > C:\Scripts_Output\NetAdapters.txt Get-NetAdapter -IncludeHidden >> C:\Scripts_Output\NetAdapters.txt
I have used only the first two redirection operators in my examples to help you understand the difference. I will not go through the full list of redirection operators as this will make the tutorial really long. The best way is to try and play with them in order to understand exactly what each operator is doing.
I hope the tutorial about PowerShell Redirection Operators is helpful.
Please let me know your comments and thoughts.
You feedback is appreciated.
- PowerShell Tutorials
- PowerShell Scripts
- about_Redirection | Microsoft Docs
- Out-File – Microsoft Docs
- Tee-Object – Microsoft Docs
- Set-Content – Microsoft Docs
- Add-Content – Microsoft Docs