Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Remove ReadOnly Attribute on File Using PowerShell?

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

like image 323
Tangiest Avatar asked May 21 '09 14:05

Tangiest


People also ask

Is a ReadOnly property PowerShell?

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.

How do I make a file read only in PowerShell?

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.


2 Answers

You can use Set-ItemProperty:

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

or shorter:

sp file.txt IsReadOnly $false 
like image 128
Joey Avatar answered Sep 23 '22 18:09

Joey


$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    }   
like image 42
Tangiest Avatar answered Sep 24 '22 18:09

Tangiest