如何在Windows中使用命令行向用户授予目录权限?
如何使用 Windows 命令行向用户授予目录权限(读、写、修改)?
How can I grant permissions to a user on a directory (Read, Write, Modify) using the Windows command line?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
从 Vista 开始,
cacls
已被弃用。以下是前几行帮助行:您应该使用
icacls
代替。这是授予 John 对D:\test
文件夹及其所有子文件夹的完全控制权的方法:根据 MS 文档:
F
= 完全控制CI
= 容器继承 - 该标志指示从属容器将继承此 ACE。OI
= 对象继承 - 该标志表示从属文件将继承 ACE。/T
= 递归地应用到现有文件和子文件夹。 (OI
和CI
仅适用于新文件和子文件夹)。信用:@AlexSpence 的评论。如需完整文档,您可以运行不带参数的“
icacls
”,或参阅 Microsoft 文档 此处 和此处As of Vista,
cacls
is deprecated. Here's the first couple of help lines:You should use
icacls
instead. This is how you grant John full control overD:\test
folder and all its subfolders:According do MS documentation:
F
= Full ControlCI
= Container Inherit - This flag indicates that subordinate containers will inherit this ACE.OI
= Object Inherit - This flag indicates that subordinate files will inherit the ACE./T
= Apply recursively to existing files and sub-folders. (OI
andCI
only apply to new files and sub-folders). Credit: comment by @AlexSpence.For complete documentation, you may run "
icacls
" with no arguments or see the Microsoft documentation here and here您还可以使用 ICACLS。
向用户组授予对文件夹的完全控制权限:
向 IIS 用户授予
C:\MyFolder
的修改权限>(如果您需要 IIS 能够将文件读/写到特定文件夹中):如果您执行ICACLS /?,您将能够看到所有可用选项。
You can also use ICACLS.
To grant the Users group Full Control to a folder:
To grant Modify permission to IIS users for
C:\MyFolder
(if you need your IIS has ability to R/W files into specific folder):If you do ICACLS /? you will be able to see all available options.
打开命令提示符,然后执行以下命令:
F
提供完全访问权限。/q /c /t
将权限应用于子文件夹。注意:有时“以管理员身份运行”会有所帮助。
Open a Command Prompt, then execute this command:
F
gives Full Access./q /c /t
applies the permissions to subfolders.Note: Sometimes "Run as Administrator" will help.
使用 cacls 命令。请参阅此处的信息。
Use
cacls
command. See information here.我尝试以下方法,它对我有用:
cmd.exe
takeown /R /F *.*
icacls * /T /grant [username]: (D)
这样文件就可以成为我自己的访问权限,并将其指定为“删除”,然后我就可以删除文件和文件夹。
I try the below way and it work for me:
cmd.exe
takeown /R /F *.*
icacls * /T /grant [username]:(D)
So that the files can become my own access and it assign to "Delete" and then I can delete the files and folders.
损坏的权限:重新获得对文件夹及其子对象的访问
尽管大多数回答该问题的答案都有一定的优点,但恕我直言,它们都没有给出完整的解决方案。如果您因权限设置损坏而锁定文件夹,以下(可能)是 Windows 7 的完美解决方案:
对于 Windows 10,必须指定用户/SID在
/remove:d
选项之后:.
注释:
该命令应用于指定目录。
指定用户“Everyone”会设置尽可能广泛的权限,因为它包括每个可能的用户。
选项“/remove:d”删除可能存在的任何显式 DENY 设置,因为这些设置会覆盖显式 ALLOW 设置:这是创建新 ALLOW 设置的必要准备。 这只是一种预防措施,因为通常不存在 DENY 设置,但安全总比后悔好。
选项“/grant”创建一个新的 ALLOW 设置,这是一个替换 (" :r") 可能存在的任何和所有显式 ALLOW 设置。
“F”参数(即创建的权限)授予完全控制权。
“/T”参数添加递归,将这些更改应用到指定目录(即文件和子文件夹)中的所有当前子对象以及文件夹本身。
“(OI)”和“(CI)”参数还添加递归,将这些更改应用于随后创建的子对象。
。
附录 (2019/02/10) -
今天向我推荐了上面的 Windows 10 命令行,所以就在这里。我还没有 Windows 10 来测试它,但如果你有的话请尝试一下(然后请在下面发表评论)。
此更改仅涉及删除 DENY 设置作为第一步。很可能不存在任何 DENY 设置,因此该选项可能没有任何区别。我的理解是,在 Windows 7 上,您不需要在 /remove:d 之后指定用户,但我可能是错的!
。
附录 (2019/11/21) -
用户 astark 建议将 Everyone 替换为术语 *S-1-1-0,以便命令与语言无关。我只安装了英文版Windows,所以我无法测试这个建议,但它似乎是合理的。
Corrupt Permissions: Regaining access to a folder and its sub-objects
Although most of the answers posted in reply to the question have some merit, IMHO none of them give a complete solution. The following (might be) a perfect solution for Windows 7 if you are locked-out of a folder by corrupted permission settings:
For Windows 10 the user/SID must be specified after the
/remove:d
option:.
Notes:
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.
.
ADDENDUM (2019/02/10) -
The Windows 10 command line above was kindly suggested to me today, so here it is. I haven't got Windows 10 to test it, but please try it out if you have (and then will you please post a comment below).
The change only concerns removing the DENY setting as a first step. There might well not be any DENY setting present, so that option might make no difference. My understanding is, on Windows 7, that you don't need to specify a user after /remove:d but I might be wrong about that!
.
ADDENDUM (2019/11/21) -
User astark recommends replacing Everyone with the term *S-1-1-0 in order for the command to be language independent. I only have an English install of Windows, so I can't test this proposal, but it seems reasonable.
我为此苦苦挣扎了一段时间,只有结合此线程中的答案才对我有用(在 Windows 10 上):
1. 打开 cmd 或 PowerShell 并转到包含文件的文件夹
2. takeown /R /F .
3. icacls * /T /grant dan:F
祝你好运!
I struggled with this for a while and only combining the answers in this thread worked for me (on Windows 10):
1. Open cmd or PowerShell and go to the folder with files
2. takeown /R /F .
3. icacls * /T /grant dan:F
Good luck!
使用 Excel vba 脚本来配置和创建帐户。我需要向新用户授予该工具使用管理员“x”帐户创建的文件夹和子文件夹的完全权限。
cacls 看起来像这样:
cacls \FileServer\Users\Username /e /g Domain\Username:C
我需要将此代码迁移到 Windows 7 及更高版本。我的解决方案是:
icacls \FileServer\Users\Username /grant:r Domain\Username:(OI)(CI)F /t
/grant:r - 授予指定的用户访问权限。权限取代先前授予的显式权限。如果没有 :r,权限将添加到任何先前授予的显式权限
(OI)(CI) - 此文件夹、子文件夹和文件。
F - 完全访问
/t - 遍历所有子文件夹以匹配文件/目录。
这给了我的是该服务器上的一个文件夹,用户只能看到该文件夹并创建子文件夹,他们可以读取和写入文件。以及创建新文件夹。
With an Excel vba script to provision and create accounts. I was needing to grant full rights permissions to the folder and subfolders that were created by the tool using our administrators 'x' account to our new user.
cacls looked something like this:
cacls \FileServer\Users\Username /e /g Domain\Username:C
I needed to migrate this code to Windows 7 and beyond. My solution turned out to be:
icacls \FileServer\Users\Username /grant:r Domain\Username:(OI)(CI)F /t
/grant:r - Grants specified user access rights. Permissions replace previously granted explicit permissions. Without :r, permissions are added to any previously granted explicit permissions
(OI)(CI) - This folder, subfolders, and files.
F - Full Access
/t - Traverse all subfolders to match files/directories.
What this gave me was a folder on this server that the user could only see that folder and created subfolders, that they could read and write files. As well as create new folders.
以防万一其他人在这个页面上绊倒,如果你想在一个命令中将各种权限串在一起,我使用了这个:
注意各种权限的 csv 字符串。
Just in case there is anyone else that stumbles on this page, if you want to string various permissions together in the one command, I used this:
Note the csv string for the various permissions.
XCACLS.VBS 是一个非常强大的脚本,可以更改/编辑 ACL 信息。 c:\windows\system32\cscript.exe xcacls.vbs 帮助返回所有开关和选项。
您可以从 Microsoft 支持页面获取官方发行版
XCACLS.VBS is a very powerful script that will change/edit ACL info. c:\windows\system32\cscript.exe xcacls.vbs help returns all switches and options.
You can get official distribution from Microsoft Support Page
批量文件夹创建和授予权限可以通过使用下面的 powershell 脚本来完成。
注意:您必须在 csv 文件中创建相同的域用户名,否则您将遇到权限问题
Bulk folder creation and grant permission works me by using the below powershell script.
Note: You have to create same domain username in csv file otherwise you will get permission issues
我无法打开驱动器中的任何文件,此命令解锁了所有 -
i was not able to open any file in a drive, this command unlocked all -
很棒的一点 Călin Darie
我有很多脚本可以使用 cacls 我将它们移至 icacls
我怎么找不到一个脚本来更改根安装卷示例:d:\datafolder。我最终创建了下面的脚本,它将卷安装为临时驱动器,然后应用秒。然后卸载它。这是我发现可以更新根安装安全性的唯一方法。
1 将文件夹安装 GUID 获取到临时文件,然后读取 GUID 以将卷安装为临时驱动器 X: 应用秒并记录更改,然后仅从 X: 驱动器卸载卷,以便安装的文件夹不会更改或中断然后应用秒。
这是我的脚本示例:
excellent point Călin Darie
I had a lot of scripts to use cacls I move them to icacls
how ever I could not find a script to change the root mount volumes example: d:\datafolder. I finally crated the script below, which mounts the volume as a temporary drive then applies sec. then unmounts it. It is the only way I found that you can update the root mount security.
1 gets the folder mount GUID to a temp file then reads the GUID to mount the volume as a temp drive X: applies sec and logs the changes then unmounts the Volume only from the X: drive so the mounted folder is not altered or interrupted other then the applied sec.
here is sample of my script:
我是管理员,某些脚本对目录中所有文件和子文件夹的我的名字设置了“拒绝”权限。执行 icacls "D:\test" /grant John:(OI)(CI)F /T 命令不起作用,因为它似乎没有从我的名字中删除“拒绝”这个清单。
唯一对我有用的是使用 icacls "D:\test" /reset /T 命令重置所有权限。
I am Administrator and some script placed "Deny" permission on my name on all files and subfolders in a directory. Executing the
icacls "D:\test" /grant John:(OI)(CI)F /T
command did not work, because it seemed it did not remove the "Deny" right from my name from this list.The only thing that worked for me is resetting all permissions with the
icacls "D:\test" /reset /T
command.icacls 。 /grant John:(OI)(CI)F /T
其中 John 是用户名只是添加这个,因为这种方式看起来非常简单,其他人可能会获利 - 所有功劳都归于
Călin Darie
。icacls . /grant John:(OI)(CI)F /T
where John is the usernameJust adding this because it seemed supremely easy this way and others may profit - all credit goes to
Călin Darie
.当我运行命令时:
folderB
中的文件都没有被处理,这是通过输出消息指示的:但是,一旦我将指定的路径更改为父目录(
"c: /path/to/folderA"
)并重新运行该命令,folderB
中的所有文件均已成功处理。注意:如果您希望不处理
folderA
中的任何其他文件/文件夹,请在运行上述命令之前尝试将所有这些文件/文件夹移动到其他位置。希望这可以帮助任何遇到同样问题的人。
When I ran the command:
None of the files in
folderB
were being processed, which was indicated via the output message:However, once I changed the specified path to the parent directory(
"c:/path/to/folderA"
) and re-ran the command all the files infolderB
were successfully processed.Note: If you want any other files/folders in
folderA
to not be processed, try moving all those files/folders to a different location before running the command above.Hope this helps anyone running into the same issue.
对于需要向多个驱动器授予权限的任何人,我创建了以下脚本:
For anyone needing to grant permissions to multiple drives, I created the following script:
在 Windows 10 中,无需“c:>”即可工作和“>”
例如:
(这也修复了错误 2502 和 2503)
in windows 10 working without "c:>" and ">"
For example:
(also this fixes error 2502 and 2503)
这对我有用:
手动打开拒绝访问的文件夹。
选择该文件夹中的可执行文件/应用程序文件。
右键单击它并转到属性 -> 兼容性
现在查看权限级别并检查以管理员身份运行
单击更改所有用户的设置。
现在问题已经解决了。
This is what worked for me:
Manually open the folder for which the access is denied.
Select the Executable/application file in that folder.
Right-click on it and go to Properties -> Compatibility
Now see the Privilege Level and check it for Run As Administrator
Click on Change Settings for all users.
The problem is solved now.