如何使用Windows命令行授予用户对目录的权限(读、写、修改)?


当前回答

损坏权限:重新获得对文件夹及其子对象的访问权

虽然这个问题的大部分答案都有一定的价值,但恕我直言,没有一个答案是完整的。下面(可能)是Windows 7的一个完美的解决方案,如果你被损坏的权限设置锁定的文件夹:

icacls "c:\folder" /remove:d /grant:r Everyone:(OI)(CI)F /T  

对于Windows 10,用户/SID必须在/remove:d选项后指定:

icacls "c:\folder" /remove:d Everyone /grant:r Everyone:(OI)(CI)F /T  

. 注:

The command is applied to the specified directory. Specifying the user "Everyone" sets the widest possible permission, as it includes every possible user. The option "/remove:d" deletes any explicit DENY settings that may exist, as those override explicit ALLOW settings: a necessary preliminary to creating a new ALLOW setting. This is only a precaution, as there is often no DENY setting present, but better safe than sorry. The option "/grant" creates a new ALLOW setting, an explicit permission that replaces (":r") any and all explicit ALLOW settings that may exist. The "F" parameter (i.e. the permission created) makes this a grant of FULL control. The "/T" parameter adds recursion, applying these changes to all current sub-objects in the specified directory (i.e. files and subfolders), as well as the folder itself. The "(OI)" and "(CI)" parameters also add recursion, applying these changes to sub-objects created subsequently. .

附录(2019/02/10)-

上面的Windows 10命令行是今天好心推荐给我的,所以就在这里。我还没有Windows 10来测试它,但是如果你有的话请尝试一下(然后请在下面发表评论)。

第一步更改只涉及删除DENY设置。可能没有任何DENY设置,所以该选项可能没有区别。我的理解是,在Windows 7上,你不需要在/remove:d后指定用户,但我可能错了!

.

附录(2019/11/21)-

用户astark建议将Everyone替换为术语*S-1-1-0,以便命令独立于语言。我只有英文版的Windows安装,所以我不能测试这个建议,但它似乎是合理的。

其他回答

Windows 10中没有“c:>”和“>”

例如:

F = Full Control
/e : Edit permission and kept old permission
/p : Set new permission

/e /p用户名:F

(这也修复了错误2502和2503)

C:\Windows\Temp /e /p用户名:F

使用cacls命令。请看这里的信息。

CACLS文件/e /p {USERNAME}:{PERMISSION} 在那里, /p:设置新的权限 /e:编辑权限并保留旧权限,即编辑ACL而不是替换它。 {USERNAME}:用户名 {PERMISSION}:权限可以是: R -读取 W -写 C -改变(写) F -完全控制 例如,用以下命令授予Rocky完全(F)控制权(在Windows命令提示符下输入): C:> CACLS files /e /p rocky:f 输入以下命令阅读完整帮助: C:> cacls /?

在Vista中,不支持使用cacls。以下是前几条帮助热线:

C:\>cacls
NOTE: Cacls is now deprecated, please use Icacls.

Displays or modifies access control lists (ACLs) of files

你应该使用icacls。这是你授予John对D:\test文件夹及其所有子文件夹的完全控制权的方式:

C:\>icacls "D:\test" /grant John:(OI)(CI)F /T

根据MS文档:

F =完全控制 CI =容器继承——该标志表示从属容器将继承此ACE。 OI = Object Inherit -该标志表示从属文件将继承ACE。 /T =递归应用到现有文件和子文件夹。(OI和CI只适用于新文件和子文件夹)。图片来源:@AlexSpence。

要获得完整的文档,您可以不带参数地运行“icacls”,或者在这里和这里查看Microsoft文档

attrib +r +a +s +h <folder name> <file name> to hide
attrib -r -a -s -h <folder name> <file name> to unhide

当我运行命令时:

icacls "c:/path/to/folderA/folderB" /grant:r Everyone:(OI)(CI)F /T

folderB中的文件都没有被处理,这是通过输出消息表示的:

Successfully processed 0 files; Failed processing 0 files

然而,一旦我将指定的路径更改为父目录(“c:/path/to/folderA”)并重新运行命令,folderB中的所有文件都被成功处理。

注意:如果您希望folderA中的任何其他文件/文件夹不被处理,请在运行上述命令之前尝试将所有这些文件/文件夹移动到不同的位置。

希望这对遇到同样问题的人有所帮助。