Changes

Jump to: navigation, search

Windows PowerShell 1.0 Pipes and Redirection

4,301 bytes added, 21:30, 1 December 2008
Windows PowerShell Redirection Operators
<tr bgcolor="#e9e9e6">
<table>
 
== 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:
 
<pre>
PS C:\Users\Administrator> get-date > date.txt
</pre>
 
The file, date.txt, now contains the output from the ''get-date'' command, as demonstrated by displaying the contents of the file:
 
<pre>
PS C:\Users\Administrator> type date.txt
 
Monday, December 01, 2008 1:11:36 PM
</pre>
 
Having created the file, it is also possible to append more output to the end of the existing content using the >> operator as follows:
 
<pre>
PS C:\Users\Administrator> get-date >> date.txt
</pre>
 
This time, the original content remains in the file, with the new output added to the end:
 
<pre>
PS C:\Users\Administrator> type date.txt
 
Monday, December 01, 2008 1:11:36 PM
 
Monday, December 01, 2008 1:13:45 PM
</pre>
 
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:
 
<pre>
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
</pre>
 
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:
 
<pre>
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
</pre>
 
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:
 
<pre>
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
</pre>
 
In this case, the error did not appear on the console. Instead, it was sent to the file named ''error.txt'':
 
<pre>
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
</pre>
 
If error output is to be discarded entirely it may be redirected to $null:
 
<pre>
PS C:\Users\Administrator> dir myfiles.txt 2> $null
</pre>
 
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:
 
<pre>
PS C:\Users\Administrator> dir mydata.txt, myfiles.txt > output.txt 2>&1
</pre>
 
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:
 
<pre>
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
</pre>

Navigation menu