Changes

Jump to: navigation, search

Windows PowerShell 1.0 File Handling

4,047 bytes added, 20:11, 8 December 2008
Changing File Properties with Set-ItemProperty
</pre>
A full list of PowerShell supported file attributes provided through [System.IO.FileAttributes] is outlined in the following table:
<table border="1" cellpadding="5" cellspacing="0" id="E3B" style="border-collapse: collapse; border-color:#cccccc; border-style: solid; border-width: 1px; margin-bottom:20px">
</tr>
</table>
 
== Reading Text Files in Windows PowerShell ==
 
Text files are read in PowerShell using the ''Get-Content'' cmdlet which is also available via the ''cat'' and ''type'' aliases. When executed with one or file names, the content of those files is directed to the output stream. The following example reads and displays the contents of a single text file:
 
<pre>
PS C:\tmp> get-content test.txt
This is a test
</pre>
 
The contents of multiple files may be read in a single command by providing a comma separated array of file names:
 
<pre>
PS C:\tmp> get-content test.txt,test2.txt,test3.txt,test4.txt
This is a test
This is a test2
This is a test3
This is a test4
</pre>
 
The ''Get-Content'' supports text files which are encoded as String, Unicode, Byte, BigEndianUnicode, UTF8, UTF7 and Ascii, selectable using the ''-encoding'' option. For example:
 
<pre>
get-content -encoding Ascii test.txt
</pre>
 
== Reading Binary Files in Windows PowerShell ==
 
Binary files are also read using the ''Get-Content'' cmdlet using the ''-encoding Byte'' option. For example, the following command reads the contents of a binary file and assigns it to a variable:
 
<pre>
$mybinfile = get-content -encoding byte c:\windows\winhelp.exe
</pre>
 
== Writing to Files in Windows PowerShell ==
 
Content is written to files in Windows PowerShell using the ''''Set-Content'' and ''Out-File'' cmdlets. Whilst both commands write data to files, each provides diferrent results. ''Set-Content'' writes the raw data to the file in the form in which it is received by the cmdlet. ''Out-File'', on the other hand, will format the output for human consumption (in much the way content is formatted by default when displayed in the Windows PowerShell console). As such, ''Out-File'' is unsuitable for writing binary files.
 
As an example of reading and writing files, the following command make a binary copy of the Windows ''winhelp.exe'' executable binary file:
 
<pre>
get-content -encoding byte winhelp.exe | set-content -encoding byte c:\tmp\myhelp.exe
</pre>
 
To append content to the end of an existing file, the ''Add-Content'' cmdlet may be used.
 
== Searching for Strings in Files with Windows PowerShell ==
 
The contents of files may be searched for specific pattern matches through the use of the ''Select-String'' cmdlet. In the simplest form, Select-String can be used to find a specific string in a single file:
 
<pre>
PS C:\tmp> select-string "this" test.txt
 
test.txt:1:This is a test
</pre>
 
By default, searches are case-insensitive. To perform a case sensitive search, the ''-caseSensitive'' option must be used:
 
<pre>
PS C:\tmp> select-string -casesensitive "this" test.txt
</pre>
 
If a directory contains multiple files, wildcards may be used in order to search multiple files in a single command:
 
<pre>
PS C:\tmp> select-string "this" *.txt
 
test2.txt:1:This is a test2
test3.txt:1:This is a test3
test4.txt:1:This is a test4
</pre>
 
If a boolean success/failure result is all that is required, rather than detailed information of the lines and files which matched the specified string, the ''-quiet'' option is used:
 
<pre>
PS C:\tmp> select-string -quiet "this" *.txt
True
</pre>
 
The pattern string may also take the form of a regular expression. For example, the following command will only find a match where a line begins with the word "This":
 
<pre>
 
PS C:\tmp> select-string "^This" *.txt
 
test2.txt:1:This is a test2
test3.txt:1:This is a test3
test4.txt:1:This is a test4
</pre>
 
To limit the results to the first match in each file, use the ''-list'' option.
 
To perform a recursive search through all sub-directories and files contained within a specific directory, use the ''Get-ChildItem'' cmdlet with the ''-recurse'' option and pipe it through ''Select-String'' as follows:
 
<pre>
PS C:\> get-childitem -recurse c:\tmp | select-string "This"
 
tmp\test2.txt:1:This is a test2
tmp\test3.txt:1:This is a test3
tmp\test4.txt:1:This is a test4
tmp\test5.txt:1:A test this is
tmp\myfiles\test.txt:1:This is a test
</pre>

Navigation menu