PowerShell 3.0 New Redirection Commands
From the beginning PowerShell has had the ability to redirect output with | (pipeline), Out-File and the old (>). Now in PowerShell 3.0 we have the ability to tweak (>) so that we can save warnings and debug information to a text file.
Windows PowerShell 3.0 Redirection Examples
- The Redirection Situation in PowerShell 2.0
- PowerShell Error Redirection with ‘2>’
- PowerShell 3.0 Redirection Improvements
- PowerShell 3.0 About_Redirection
- The Mystery of PowerShell >2&1
In addition to my favourite | Out-File, PowerShell supports redirection with the ubiquitous > (greater than symbol). Let us see an example:
# PowerShell Redirection with >
Get-Process > D:\PShell\Proc0.txt
Note 1: I prefer Get-Process | Out-File D:\PShell\Proc1.txt
Note 2: The double chevron >> means append, whereas the single > means over-write.
Left to its own defaults, PowerShell directs its results to the console. There are many occasions where you would prefer to save the output as text to be stored in a file. This page concentrates on the latest redirection techniques.
Let us be clear, this is a technique for saving errors generated by PowerShell scripts to a file.
There are two keys to getting action:
a) Introduce a non-existent process called xyz, or some such gibberish.
b) The carefully insert 2> and not 2 >. A space here kills the command. Also beware of the wrong sequence, this won’t work >2.
# PowerShell’s way of redirecting errors
Get-Process xyz 2> D:\PShell\Proc2.txt
Note 3: A double chevron >> would means append, whereas the single > means over-write.
PowerShell 3.0 offers the ability to fine-tune redirection by using not only 2>, but also 3> and 4>. Incidentally, there is also a 1> but I cannot see how it improves on plain >.
Pipeline 1> (Same as plain >)
Error 2> (Same as PowerShell 2.0)
Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems. Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.
Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try – it’s free.
This is one of those topics where PowerShell provides an ‘About’ help file. To see more try this:
Help tells us that we can add errors to the success stream with >2&1. This is also called merging streams. However, help appears to leave out a crucial element, you need an initial > to redirect, then PowerShell’s >2&1 at the end thus:
# PowerShell’s way of merging errors
Get-Process svchost > D:\PShell\Proc2.txt >2&1
Note 4: My mistake was trying Get-Process svchost >2&1 D:\PShell\Proc2.txt
To be fair, I have seen other example:
get-process svchost, powershell 2>&1 | Out-File Svchost.txt
Conclusion: Merging streams with & is not as intuitive as I first thought.
An Alternative to Redirection Commands | Out-File
It could be that I am ‘hard-cooked’, I cannot seem to abandon the habit of using PowerShell’s Out-File; I am finding it hard switching to using these > and >> redirection commands. Perhaps I don’t generate enough errors to warrant the use of the 2> redirector.
That said, I have filed away in my memory bank the idea of redirection warning messages with 3>.
PowerShell Out-File Example
Get-Service | Out-File D:\Pshell\Services.txt -force
Note 5: Out-File supports switches such as -force that you don’t get with redirection.
Summary of PowerShell 3.0 Redirection
Here is a case where a new PowerShell 3.0 command showed me a technique that I had overlooked in PowerShell 2.0, namely saving errors to a text file. Another way of looking at PowerShell 3.0’s redirection is that it has now come of age with the introduction of 3> warning and 4> debugging commands.
If you like this page then please share it with your friends
See more Microsoft PowerShell output tutorials:
Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.