Skip Navigation LinksHome > View Post

How to remove a file attribute in PowerShell

Or, in this case, how to make a file not readonly in powershell. It took me a while to get this working and this is my best effort. In fact, this particular example recurses a whole folder and makes all files not readonly

$readonly = [System.IO.FileAttributes]::ReadOnly
get-childitem . -recurse | foreach { if ($_.Attributes -band $readonly -eq $readonly) { $_.Attributes -= $readonly.value__ } }

I did try to set the IsReadOnly property but Powershell throws a wobbly at that. If anybody knows a neater way of doing this I'd love to know.

Tags: Other

 
Josh Post By Josh Twist
4:37 AM
27 Jun 2008

» Next Post: Creating AD user accounts in PowerShell
« Previous Post: What's next for Ukadc.Diagnostics?

Comments are closed for this post.

Posted by Adrian Bateman @ 27 Jun 2008 7:14 AM
Why Powershell? I mean, I like Powershell and all, but why wouldn't you just use attrib?

Posted by Josh @ 28 Jun 2008 10:41 AM
Why not use attrib?

If you don't know about it :)

That's much neater:

get-childitem <folder> -recurse | attrib -R

or

attrib -R /S

to do it for the whole current folder

Posted by Adrian Bateman @ 30 Jun 2008 2:14 PM
Ah, that'd do it. :o)

Two weeks ago I could have given you my DOS 3.3 manuals to gen up on this stuff. Unfortunately I no longer own them. Oh well!

Posted by Sam @ 20 Mar 2009 12:09 PM
Clear-ItemProperty C:\Temp\webproduction\elcabop.vsmdi -Name attributes -force

Posted by Sam @ 20 Mar 2009 12:11 PM
Clear-ItemProperty C:\Temp\webproduction\elcabop.vsmdi -Name attributes -force

Posted by Exotic Hadron @ 31 Mar 2011 2:30 PM
That would be sorta obsolete answer but for history sake...
The following line set off the Read only property for all the files with extension *.SLN located under the C:\folder\ and its subfolders:

Get-ChildItem "C:\folder\" -r *.sln | ForEach {$_.Set_IsReadOnly($False)}

© 2005 - 2014 Josh Twist - All Rights Reserved.