Summary: Microsoft Scripting Guy Ed Wilson talks about using the new Windows PowerShell redirectioin operators to add flexibility to a script.
Hey, Scripting Guy! There is something about Windows PowerShell that I don’t get. Normally, I can use the redirection arrows to write to a text file, but sometimes it does not work. Is this a bug in Windows PowerShell, or is there something I don’t understand (probably)?
Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife and I spent much of last night working on our three-week European tour itinerary. We will be visiting a number of Windows PowerShell User Groups, and seeing even more Windows PowerShell MVPs. This morning, the Scripting Wife emailed our working itinerary to our various contacts. Stay tuned. Like The Monkeys, we may be coming to your town.
Understanding various output streams
Understanding output might sound complicated, but it is actually easy. Windows PowerShell has various output streams. What most beginning Windows PowerShell users think of as output is only one output stream—the success output; but there are many output streams:
- Success output
- Error message output
- Warning message output
- Verbose message output
- Debug message output
Note The descriptions of and uses for the various redirection operators are explained in the about_Redirection topic in the TechNet Library. Refer to that topic for a great chart that details all of the redirection operators.
In Windows PowerShell 3.0, there are three new redirection operators. These operators are shown here.
Like other redirection operators, there are the overwrite (>) and the append (>>) versions. The default values for the different streams (configured as preference variables) display errors and warnings, but not verbose and debug. This is shown here.
PS C:\> dir variable: -Include "*preference" -Recurse
When a default preference variable receives a SilentlyContinue value, it means that the stream does not display to the host. Instead, Windows PowerShell silently continues past the command to the next command. When the value is Continue, it means the stream displays to the host, and Windows PowerShell continues to the next command.
To change a value temporarily, I add a value assignment at the top of a Windows PowerShell script. The change affects the host as long as the host remains open. When the host is closed, it reverts to normal. To make the change “permanent,” add the value to the Windows PowerShell profile.
In the DemoRedirectionStream.ps1 script, I first set three preference variables as shown here.
$VerbosePreference = "continue"
$WarningPreference = "continue"
$DebugPreference = "continue"
Next I use the Write-Warning, Write-Verbose, and the Write-Debug cmdlets to emit text strings through the various output streams. The first Write-Warning does not redirect to a text file, and it appears to the Windows PowerShell host. The remaining commands redirect to various text files, and they do not appear in the Windows PowerShell host output. This is shown in the following image.
The three output text files are shown here.
TL, that is all there is to using Windows PowerShell 3.0 redirection operators. Join me tomorrow as I talk about more Windows PowerShell. It will be cool!
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy