How can I remove the ReadOnly attribute on a file, using a PowerShell (version 1.0) script?
Although Powershell doesn't have real readonly class properties, we can mimic them in two elegant ways: Class methods as getter and setter functions. Script properties with getter and setter functions.
Here's a fast PowerShell tip. Need to mark a file as Read-Only? Use Get-ChildItem to get the file and then invoke the Set_IsReadOnly method. This method needs a Boolean value (True or False) to indicate whether a file is ReadOnly.
You can use Set-ItemProperty
:
Set-ItemProperty file.txt -name IsReadOnly -value $false
or shorter:
sp file.txt IsReadOnly $false
$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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With