How to Remove ReadOnly Attribute on File Using PowerShell?

后端 未结 6 1302
无人及你
无人及你 2020-12-08 01:50

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

6条回答
  •  借酒劲吻你
    2020-12-08 02:03

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

提交回复
热议问题