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.
PowerShell Pipes
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>
Operator | Description |
---|---|
> | 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. |
Windows PowerShell Redirection
The operators outlined above are, perhaps, best demonstrated using some examples, the first of which sends output to a file, deleting any pre-existing content:
PS C:\Users\Administrator> get-date > date.txt
The file, date.txt, now contains the output from the get-date command, as demonstrated by displaying the contents of the file:
PS C:\Users\Administrator> type date.txt Monday, December 01, 2008 1:11:36 PM
Having created the file, it is also possible to append more output to the end of the existing content using the >> operator as follows:
PS C:\Users\Administrator> get-date >> date.txt
This time, the original content remains in the file, with the new output added to the end:
PS C:\Users\Administrator> type date.txt Monday, December 01, 2008 1:11:36 PM Monday, December 01, 2008 1:13:45 PM
As mentioned previously, other shell environments allow input to be redirected. This would ordinarily be achieved using the < operator to read input from a file or even the keyboard. As of version 1.0 of Windows PowerShell this feature has not been implemented, although there is every reason to expect it will appear in subsequent versions.
Redirecting Error Output
Windows PowerShell has the concept of different output streams for standard output and error messages. The main purpose of this is to prevent error messages from being included within legitimate output. In the following example, only the valid output is redirected to the file. Since we have not redirected the error output, it is displayed in the console:
PS C:\Users\Administrator> dir mydata.txt, myfiles.txt > error.txt Get-ChildItem : Cannot find path 'C:\Users\Administrator\myfiles.txt' because it does not exist. At line:1 char:4 + dir <<<< mydata.txt, myfiles.txt > output.txt
In the above console output, PowerShell is telling us in the error message that the file named myfiles.txt does not exist in the current directory. Since there was no complaint about the mydata.txt file it is safe to assume that part of the command worked and we should expect some valid output to have been written to the output.txt file:
PS C:\Users\Administrator> type output.txt Directory: Microsoft.PowerShell.Core\FileSystem::C:\Users\Administrator Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 12/1/2008 12:39 PM 30 mydata.txt
An alternative approach is to redirect the error output to a file so that it is not visible in the console using the 2> redirection operator:
PS C:\Users\Administrator> dir mydata.txt, myfiles.txt 2> error.txt Directory: Microsoft.PowerShell.Core\FileSystem::C:\Users\Administrator Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 12/1/2008 12:39 PM 30 mydata.txt
In this case, the error did not appear on the console. Instead, it was sent to the file named error.txt:
PS C:\Users\Administrator> type error.txt Get-ChildItem : Cannot find path 'C:\Users\Administrator\myfiles.txt' because it does not exist. At line:1 char:4 + dir <<<< mydata.txt, myfiles.txt 2> error.txt
If error output is to be discarded entirely it may be redirected to $null:
PS C:\Users\Administrator> dir myfiles.txt 2> $null
The 2>&1 operator redirects the error stream to the standard output stream, enabling both the regular output and any error messages to be directed to the same file:
PS C:\Users\Administrator> dir mydata.txt, myfiles.txt > output.txt 2>&1
If we now take a look at the contents of the output.txt it is clear that output to both streams was redirected to the file:
PS C:\Users\Administrator> type output.txt Directory: Microsoft.PowerShell.Core\FileSystem::C:\Users\Administrator Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 12/1/2008 12:39 PM 30 mydata.txt Get-ChildItem : Cannot find path 'C:\Users\Administrator\myfiles.txt' because it does not exist. At line:1 char:4 + dir <<<< mydata.txt, myfiles.txt > output.txt 2>&1<google>BUY_WPS_BOTTOM</google>