How to Remove ReadOnly Attribute on File Using PowerShell?

PowershellPowershell 1.0Scripting

Powershell Problem Overview


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

Powershell Solutions


Solution 1 - Powershell

You can use Set-ItemProperty:

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

or shorter:

sp file.txt IsReadOnly $false

Solution 2 - Powershell

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

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

attrib -R file.txt

Solution 4 - Powershell

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.

Solution 5 - Powershell

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

Solution 6 - Powershell

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTangiestView Question on Stackoverflow
Solution 1 - PowershellJoeyView Answer on Stackoverflow
Solution 2 - PowershellTangiestView Answer on Stackoverflow
Solution 3 - PowershellScott SaadView Answer on Stackoverflow
Solution 4 - PowershellMariusz GorzochView Answer on Stackoverflow
Solution 5 - PowershellKeith HillView Answer on Stackoverflow
Solution 6 - PowershellredzView Answer on Stackoverflow