Windows PowerShell 1.0 Pipes and Redirection
|Previous||Table of Contents||Next|
|Windows PowerShell 1.0 Comparison and Containment Operators||Windows PowerShell 1.0 Flow Control with if, else and elseif|
|Purchase and download the full PDF version of this PowerShell eBook for only $8.99|
Two features of PowerShell which will no doubt be familiar to users of UNIX and Linux based shell environments are pipes and redirection. The primary purpose of pipes is to chain commands together, passing the output from one command through to the next command. Redirection, on the other hand, allows the output from a command to be sent to a file. Unfortunately, whilst UNIX and Linux based shells allow input to be redirected to a commands (such as input from the keyboard), version 1.0 of PowerShell does not yet support this feature.
Pipelines in PowerShell are essentially a sequence of commands in which the result of each command is passed through to the subsequent command for processing. One point to note is that, unlike other shell environments, the result passed from one command to the next need not be a string, in fact it can be any type of object. Each command in a pipe is separated by the pipe character (|).
A common example of the use of pipes involves piping output from a command through to a second command which in turn formats that output. In the following example the output from the Get-Childitem command is piped through to the format-table command:
PS C:\Users\Administrator> get-childitem mydata.txt | fl Directory: Microsoft.PowerShell.Core\FileSystem::C:\Users\Administrator Name : mydata.txt Length : 30 CreationTime : 11/14/2008 12:33:23 PM LastWriteTime : 12/1/2008 12:39:44 PM LastAccessTime : 11/14/2008 12:33:23 PM VersionInfo :
Windows PowerShell Redirection Operators
The operators implemented by Windows PowerShell to facilitate redirection are similar to those used in other shell environments. The full complement of these operators is outlined in the following table: <google>ADSDAQBOX_FLOW</google>
|>||Redirects output to specified file. If the file already exists, current contents are overwritten.|
|>>||Redirects output to specified file. If the file already exists, the new output is appended to the current content.|
|2>||Redirects error output to specified file. If the file already exists, current contents are overwritten.|
|2>>||Redirects error output to specified file. If the file already exists, the new output is appended to the current content.|
|2>&1||Redirects error output to the standard output pipe instead of to the error output pipe.|