How to Remove ReadOnly Attribute on File Using PowerShell?

Solution 1:

You can use Set-ItemProperty:

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

or shorter:

sp file.txt IsReadOnly $false

Solution 2:

$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   
}  

Solution 3:

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

attrib -R file.txt

Solution 4:

or you can simply use:

get-childitem *.cs -Recurse -File | % { $_.IsReadOnly=$false }

Above will work for all .cs files in sub-tree of current folder. If you need other types included then simply adjust "*.cs" to your needs.