Changes

Jump to: navigation, search

Windows PowerShell 1.0 File Handling

2,952 bytes added, 15:55, 8 December 2008
New page: 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 of h...
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 of how to create and manipulate individual files using the PowerShell environment.

== Getting File Properties ==

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

<pre>
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
</pre>

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:

<pre>
PS C:\tmp> $myfile.lastaccesstime

Monday, December 08, 2008 6:59:48 AM
</pre>

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:

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

== Changing File Properties ===

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:

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

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:

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

A full list of 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 bgcolor="#cccccc" style="color:black" valign="top">
<th>
<p>Member</p>
</th>
<th>
<p>Description</p>
</th>
</tr>
<tr>
<td>ReadOnly <td>The file is read-only.</td>
</tr>

<tr bgcolor="#e9e9e6">
<td>Hidden <td>The file is hidden, and thus is not included in an ordinary directory listing.</td>
</tr>

<tr>
<td>System <td>The file is a system file. The file is part of the operating system or is used exclusively by the operating system.</td>
</tr>

<tr bgcolor="#e9e9e6">
<td>Archive <td>The file's archive status. Applications use this attribute to mark files for backup or removal.</td>
</tr>

<tr>
<td> Normal <td>The file is normal and has no other attributes set. This attribute is valid only if used alone.</td>
</tr>
</table>

Navigation menu