102

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

0

7 Answers 7

148

You can use Set-ItemProperty:

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

or shorter:

sp file.txt IsReadOnly $false
3
  • 3
    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
    May 27, 2009 at 14:45
  • Using PowerShell v2 I'm seeing hard-to-use CmdLet bindngs for sp. PSCX Set-Writable and Set-ReadOnly don't have those problems. I'll blog the problems I'm seeing and link to it later. I recommend Keith's answer for PowerShell v2 (modern PowerShell).
    – yzorg
    Apr 21, 2011 at 22:39
  • 3
    @yzorg: So what exactly are you telling me here? As for Keith's answer, he's using PSCX. Not everyone has those installed and that's not really a case of PowerShell v1 vs. v2.
    – Joey
    Apr 22, 2011 at 9:28
18
$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   
}  
1
  • 6
    The implementation is simpler than that: $file.IsReadOnly = $false
    – Keith Hill
    May 21, 2009 at 15:04
16

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

attrib -R file.txt
2
  • Thanks! This worked for me: dir . -r *.cs | % { $_.fullname } | % { attrib -r $_ } Dec 7, 2012 at 23:22
  • 1
    Could have skipped a step: dir . -r *.cs | % { attrib -r $_.FullName } Nov 8, 2013 at 20:15
12

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.

1
  • 4
    Adjust the command (add -File) to only work with files. Directories do not have the property. Jan 21, 2015 at 17:44
7

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
2
Shell("net share sharefolder=c:\sharefolder/GRANT:Everyone,FULL")
Shell("net share sharefolder= c:\sharefolder/G:Everyone:F /SPEC B")
Shell("Icacls C:\sharefolder/grant Everyone:F /inheritance:e /T")
Shell("attrib -r +s C:\\sharefolder\*.* /s /d", AppWinStyle.Hide)

thanks for anybody who are helping to solved some problem...and helping this code

this code is working for me.. to share a folder to every one with read and write permission you can use this in .net

1

None of the solutions above change the READONLY status of folders and files but this PowerShell script does based on the following powershell commands.

Query the READONLY status of a folder (via the DirectoryInfo object)

$roStatus = $dirInfo.Attributes -match 'ReadOnly'

Change the READONLY status of a folder (via the DirectoryInfo object)

$dirInfo.Attributes += 'ReadOnly'
$dirInfo.Attributes -= 'ReadOnly'

Query the READONLY status of a file (using the file name)

$roStatus = Get-ItemPropertyValue -Path $strFileName -Name IsReadOnly

Change the READONLY status of a file (using the file name)

Set-ItemProperty -Path $strFileName -Name IsReadOnly -Value $false
1
  • DO NOT use += or -= because it will do calculations on the attribute value and result in wrong attribute if any other bit is set already leading to files that seem to disappear or have entirely different flags set.
    – Alex
    Mar 6 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.