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.
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) Warning 3> Debug
4>
Guy
Recommends: WMI Monitor and It's Free!
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft 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.
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>.
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
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.
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems.
Fortunately, SolarWinds
have created the
Free WMI Monitor so that you can actually see and understand these gems of
performance information. Take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.