2

#PSCXTip Manipulating file attributes

When dealing with files under source control it is quite often useful to be able to make a bunch of files readonly or writable in bulk. You can do this fairly easily in PowerShell 3.0:

PS> Get-ChildItem . –Recurse –File | Foreach {$_.IsReadOnly = $false}

However the PowerShell Community Extensions (1.2 or higher) provides an even easier way to do this common operation with two commands Set-ReadOnly (alias sro) and Set-Writable (alias swr). The above operation simplifies to this:

PS> Get-ChildItem . –Recurse –File | Set-ReadOnly

If you only need to make a set of files in the current directory readonly or writable, you can use this form:

PS> Set-Writable *.cs

Or using the aliases:

PS> swr *.cs; sro *.csproj

One final common file manipulation is to “touch” a file to set its last write time to the current time (or any specified time). This can be done with the PSCX command Set-FileTime (alias touch) e.g.:

PS> Set-FileTime *.cs

Or using the alias:

PS> touch *.cs

If you need to set the last write time to a specific time you can use the Time parameter e.g.:

PS> Get-ChildItem . –r –File | Set-FileTime -Time ((Get-Date).AddDays(-14))

By default Set-FileTime updates the last write and last accessed times. You can specify the –Created switch to update the file’s creation time.

Note: There are many more useful PowerShell Community Extensions (PSCX) commands. If you are interested in this great community project led by PowerShell MVPs Keith Hill and Oisin Grehan, give PSCX a try at http://pscx.codeplex.com.

Filed in: Columns, Tips and Tricks Tags: , , ,

2 Responses to "#PSCXTip Manipulating file attributes"

Leave a Reply

Submit Comment

© 2016 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: