Windows PowerShell 1.0 File Handling

Follow Techotopia on Twitter Bookmark and Share

From Techotopia

Jump to: navigation, search
PreviousTable of ContentsNext
Working with File Systems in Windows PowerShell 1.0An Overview of Windows PowerShell 1.0 and .NET


Purchase and download the full PDF version of this PowerShell eBook for only $8.99  Add to Cart


The previous chapter of Windows PowerShell 1.0 Essentials covered the basics of working with file systems using Windows PowerShell. This chapter is designed to provide information on how to create and manipulate individual files using the PowerShell environment.

Contents



[edit] Getting File Properties

Basic information about a particular file is available through the Get-Item and Get-ItemProperty cmdlets:

PS C:\tmp> get-item test.txt


    Directory: Microsoft.PowerShell.Core\FileSystem::C:\tmp


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         12/5/2008  12:12 PM         34 test.txt

Both of the above commands return a FileInfo object, from which specific file attributes may subsequently be extracted. For example, to obtain just the last write time:

PS C:\tmp> $myfile.lastaccesstime

Monday, December 08, 2008 6:59:48 AM

In fact, the FileInfo object provides a vast number of methods and properties, a full listing of which can be viewed using the get-member cmdlet:

PS C:\tmp> get-item test.txt | get-member

[edit] Changing File Properties with Set-ItemProperty

The properties of a file are changed in Windows PowerShell using the Set-ItemProperty cmdlet together with the appropriate .NET [System.IO.FileAttributes] static member. For example, to set the ReadOnly attribute on a file:

PS C:\tmp> set-itemproperty test.txt -name attributes -value ([System.IO.FileAttributes]::ReadOnly)

Multiple properties may be set in a single command, providing that all the properties are joined using bxor operations. In the following example, the Hidden and ReadOnly attributes are set in a single operation:

PS C:\tmp> set-itemproperty test.txt -name attributes -value ([System.IO.FileAttributes]::ReadOnly -bxor 
[System.IO.FileAttributes]::Hidden)

A full list of PowerShell supported file attributes provided through [System.IO.FileAttributes] is outlined in the following table:

Member

Description

ReadOnly File is read-only.
Hidden File is hidden, and thus is not included in an ordinary directory listing.
System File is a system file. The file is part of the operating system or is used exclusively by the operating system.
Archive File archive status. Used to mark files for backup or removal.
Normal The file is normal and has no other attributes set. Cannot be used in conjunction with other attributes.

[edit] 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:

PS C:\tmp> get-content test.txt
This is a test

The contents of multiple files may be read in a single command by providing a comma separated array of file names:

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

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:

get-content -encoding Ascii test.txt

[edit] 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:

$mybinfile = get-content -encoding byte c:\windows\winhelp.exe

[edit] 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 different 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:

get-content -encoding byte winhelp.exe | set-content -encoding byte c:\tmp\myhelp.exe

To append content to the end of an existing file, the Add-Content cmdlet may be used.

[edit] 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:

PS C:\tmp> select-string "this" test.txt

test.txt:1:This is a test

By default, searches are case-insensitive. To perform a case sensitive search, the -caseSensitive option must be used:

PS C:\tmp> select-string -casesensitive "this" test.txt

If a directory contains multiple files, wildcards may be used in order to search multiple files in a single command:

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

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:

PS C:\tmp> select-string -quiet "this" *.txt
True

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":


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

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:

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


Purchase and download the full PDF version of this PowerShell eBook for only $8.99

Add to Cart

Views
Personal tools

Find us on Facebook