Hi,
How can I remove the ReadOnly attribute on a file, using a PowerShell (version 1.0) script?
Thanks, MagicAndi
Hi,
How can I remove the ReadOnly attribute on a file, using a PowerShell (version 1.0) script?
Thanks, MagicAndi
$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
}
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
You can use Set-ItemProperty
:
Set-ItemProperty file.txt -name IsReadOnly -value $false
or shorter:
sp file.txt IsReadOnly $false
Even though it's not Native PowerShell, one can still use the simple Attrib command for this:
attrib -R file.txt