如何通过powershell删除文件夹的只读属性? [英] How to remove read-only attribute of a folder by powershell?

查看:57
本文介绍了如何通过powershell删除文件夹的只读属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了很多代码,但仍然无法删除该特定文件夹的只读属性.

I have tried many code but still I am not able to remove read-only property of that particular folder.

下面是删除该文件夹下文件的只读属性但不删除该文件夹的只读属性的代码:

Below is the code which removes read-only property of the files present under that folder but does not remove read-only attribute of that Folder :

$Path = "C:\Suraj\powershell scripts\review script" 
$Files = Get-ChildItem $Path -Recurse
ForEach ($File in $Files) {
    Write-Host file:$File IsReadOnly: $File.IsReadOnly 
    if ($File.Attributes -ne "Directory" -and $File.Attributes -ne "Directory, Archive") {
        try {
            Set-ItemProperty -Path $Path"\"$File -name IsReadOnly -value $false 
        }
        catch { 
            Write-Host "Error at file " $Path "\" $File 
        }
    } 
}

推荐答案

如果文件夹具有 ReadOnly 属性可以通过以下方式验证:

If a folder has the ReadOnly attribute can be verified with:

$folder = Get-Item -Path path/to/folder
$folder.Attributes

默认输出为:

目录

要添加 ReadOnly 属性,只需执行:

To add the ReadOnly attribute, just execute:

$folder.Attributes += 'ReadOnly'

如果再次显示属性,它应该如下所示:

If you display the attributes again, it should look like this:

只读,目录

要删除 ReadOnly 属性,只需执行:

To remove the ReadOnly attribute, just execute:

$folder.Attributes -= 'ReadOnly'

属性看起来像那样:

目录

如您所见,确实可以添加和删除 ReadOnly 属性,但正如其他人在评论中已经提到的那样,它不会产生太大影响.

As you can see, it is indeed possible to add and remove the ReadOnly attribute, but as others already mentioned in the comments, it won't have much of an effect.

这篇关于如何通过powershell删除文件夹的只读属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆