views:

1193

answers:

4

Hi,

How can I remove the ReadOnly attribute on a file, using a PowerShell (version 1.0) script?

Thanks, MagicAndi

+2  A: 
$file = Get-Item "C:\Temp\Test.txt"

if ($file.attributes -band [system.IO.FileAttributes]::ReadOnly)  
{  
  $file.attributes = $file.attributes -bxor [system.IO.FileAttributes]::ReadOnly    
}

The above code snippet is taken from this article

UPDATE Using Keith Hill's implementation from the comments (I have tested this, and it does work), this becomes:

$file = Get-Item "C:\Temp\Test.txt"

if ($file.IsReadOnly -eq $true)  
{  
  $file.IsReadOnly = $false   
}
MagicAndi
The implementation is simpler than that: $file.IsReadOnly = $false
Keith Hill
+2  A: 

If you happen to be using the PowerShell Community Extensions:

PS> Set-Writable test.txt
PS> dir . -r *.cs | Set-Writable
# Using alias swr
PS> dir . -r *.cs | swr

You can do the opposite like so:

PS> dir . -r *.cs | Set-ReadOnly
# Using alias sro
PS> dir . -r *.cs | sro
Keith Hill
+9  A: 

You can use Set-ItemProperty:

Set-ItemProperty file.txt -name IsReadOnly -value $false

or shorter:

sp file.txt IsReadOnly $false
Joey
Set-Property is the only built-in way you can cleanly do it on the pipeline, and using wildcards: { sp *.txt IsReadOnly $false } OR { ls . -recurse -include *.cs | sp -name IsReadOnly -value $false }
Jaykul
A: 

Even though it's not Native PowerShell, one can still use the simple Attrib command for this:

attrib -R file.txt
Scott Saad