Prof. Powershell

File Attributes in PowerShell

Fun with file and folder attributes, via PowerShell and the DIR command.

In PowerShell, when you run the DIR command, you are really running the Get-ChildItem cmdlet. This cmdlet returns file and folder objects; specifically System.IO.FileSystem and System.IO.DirectoryInfo respectively. When you run a command the output includes a mode column:

PS C:\work> dir *.txt

Directory: C:\work

Mode         LastWriteTime   Length Name
----         -------------   ------ ----
-a---    2/10/2012 8:21 AM   322158 a.txt
-a---    10/5/2011 8:37 PM   322174 a2.txt
-a---    2/15/2011 7:36 AM     5444 add-perm.txt

Mode is actually showing the collection of file or folder attributes. But you can also get these attributes directly:

PS C:\work> dir a*.txt | select Name,Attributes

Name                                 Attributes
----                                 ----------
a.txt                                   Archive
a2.txt                                  Archive
...

The attributes are actually a special type of object. I'm going to get a hidden folder, which is why I'm using -Force:

PS C:\work> $file=get-item hideme.txt -Force
PS C:\work> $file.attributes.GetType().Name
FileAttributes
PS C:\work> $file.attributes
ReadOnly, Hidden, Archive

If you want to filter on one of these properties, use the -Match operator:

PS C:\work> dir -force | where {$_.attributes -match "ReadOnly"}

Directory: C:\work

Mode         LastWriteTime  Length Name
----         -------------  ------ ----
d-r--   10/13/2011 7:04 PM         MyBriefCase
-arh-    2/24/2012 1:04 PM      40 hideme.txt
-ar--    8/25/2011 3:52 PM      32 hl.txt
--r--    8/4/2011 10:35 PM     191 ipdata.csv

You can also easily change a file or folder attribute. I'm going to create a new folder and then make it hidden:

PS C:\work> $h=mkdir SecretSauce
PS C:\work> $h.attributes
Directory
PS C:\work> $h.attributes="Hidden"
PS C:\work> $h.attributes
Hidden, Directory

In this particular case all I needed was to add the new attribute. But now it is hidden:

PS C:\work> get-item .\SecretSauce -force

Directory: C:\work

Mode        LastWriteTime Length Name
----        ------------- ------ ----
d--h-   2/24/2012 1:14 PM        SecretSauce

I can also easily modify attributes on files. Here's a hidden file:

PS C:\work> $f=get-item .\hideme.txt -Force
PS C:\work> $f

Directory: C:\work

Mode        LastWriteTime  Length Name
----        -------------  ------ ----
-arh-   2/24/2012 1:04 PM      40 hideme.txt

Now I'll unhide it:

PS C:\work> $f.Attributes="Archive","ReadOnly"
PS C:\work> $f

Directory: C:\work

Mode        LastWriteTime  Length Name
----        -------------  ------ ----
-ar--   2/24/2012 1:04 PM      40 hideme.txt

If you want to modify a group of files, you'll need to use a ForEach construct, but it is pretty simple.

PS C:\work> dir p*.zip

Directory: C:\work

Mode         LastWriteTime  Length Name
----         -------------  ------ ----
-a---    5/12/2011 4:18 PM  823853 prs3.zip
-a---    5/12/2011 6:43 PM 1211529 prs4.zip
-a---    5/12/2011 4:06 PM  418140 psr-demo.zip
-a---    5/12/2011 4:09 PM  945556 psr2.zip
-a---    5/12/2011 6:02 PM   67793 psr5.zip
-a---    5/12/2011 6:07 PM  136189 psr6.zip

PS C:\work> dir p*.zip | foreach {$_.Attributes="Archive","ReadOnly"}
PS C:\work> dir p*.zip

Directory: C:\work

Mode         LastWriteTime    Length Name
----         -------------    ------ ----
-ar--    5/12/2011 4:18 PM    823853 prs3.zip
-ar--    5/12/2011 6:43 PM   1211529 prs4.zip
-ar--    5/12/2011 4:06 PM    418140 psr-demo.zip
-ar--    5/12/2011 4:09 PM    945556 psr2.zip
-ar--    5/12/2011 6:02 PM     67793 psr5.zip

Just remember to set attributes to a comma-separated list. If you aren't sure what the right values are, you can ask PowerShell:

PS C:\work> [enum]::GetNames("system.io.fileattributes")
ReadOnly
Hidden
System
Directory
Archive
Device
Normal
Temporary
SparseFile
ReparsePoint
Compressed
Offline
NotContentIndexed
Encrypted

However some of these, like Compressed, are only set after something has been done to the file.

So if you find yourself fighting a backup problem, turn to PowerShell and make sure you're aA's are in order.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular

Upcoming Training Events