简体   繁体   English

如何在 Windows 中使用命令行向用户授予目录权限?

[英]How to grant permission to users for a directory using command line in Windows?

How can I grant permissions to a user on a directory (Read, Write, Modify) using the Windows command line?如何使用 Windows 命令行向用户授予目录权限(读、写、修改)?

As of Vista, cacls is deprecated.从 Vista 开始,不推荐使用cacls Here's the first couple of help lines:这是前几条帮助热线:

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

Displays or modifies access control lists (ACLs) of files

You should use icacls instead.您应该改用icacls This is how you grant John full control over D:\\test folder and all its subfolders:这就是您授予 John 对D:\\test文件夹及其所有子文件夹的完全控制权的方式:

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

According do MS documentation:根据 do MS 文档:

  • F = Full Control F = 完全控制
  • CI = Container Inherit - This flag indicates that subordinate containers will inherit this ACE. CI = Container Inherit - 此标志表示从属容器将继承此 ACE。
  • OI = Object Inherit - This flag indicates that subordinate files will inherit the ACE. OI = Object Inherit - 此标志表示从属文件将继承 ACE。
  • /T = Apply recursively to existing files and sub-folders. /T = 递归地应用于现有文件和子文件夹。 ( OI and CI only apply to new files and sub-folders). OICI仅适用于新文件和子文件夹)。 Credit: comment by @AlexSpence.信用:@AlexSpence 的评论。

For complete documentation, you may run " icacls " with no arguments or see the Microsoft documentation here and here对于完整的文档,您可以不带参数运行“ icacls ”或在此处此处查看 Microsoft 文档

You can also use ICACLS.您也可以使用 ICACLS。

To grant the Users group Full Control to a folder:授予用户组对文件夹的完全控制权限

>icacls "C:\MyFolder" /grant Users:F

To grant Modify permission to IIS users for C:\\MyFolder (if you need your IIS has ability to R/W files into specific folder):向 IIS 用户授予C:\\MyFolder修改权限(如果您需要您的 IIS 能够将 R/W 文件放入特定文件夹):

>icacls "C:\MyFolder" /grant IIS_IUSRS:M

If you do ICACLS /?如果你做ICACLS /? you will be able to see all available options.您将能够看到所有可用的选项。

Open a Command Prompt, then execute this command:打开命令提示符,然后执行以下命令:

icacls "c:\\somelocation\\of\\path" /q /c /t /grant Users:F

F gives Full Access. F提供完全访问权限。

/q /c /t applies the permissions to subfolders. /q /c /t将权限应用于子文件夹。

Note: Sometimes "Run as Administrator" will help.注意:有时“以管理员身份运行”会有所帮助。

Use cacls command.使用cacls命令。 See information here .请参阅此处的信息。

CACLS files /e /p {USERNAME}:{PERMISSION} CACLS 文件 /e /p {USERNAME}:{PERMISSION}

Where,在哪里,

/p : Set new permission /p : 设置新权限

/e : Edit permission and kept old permission as it is ie edit ACL instead of replacing it. /e :编辑权限并保留旧权限,即编辑ACL而不是替换它。

{USERNAME} : Name of user {USERNAME} :用户名

{PERMISSION} : Permission can be: {PERMISSION} :权限可以是:

R - Read R - 读取

W - Write W - 写

C - Change (write) C - 改变(写)

F - Full control F - 完全控制

For example grant Rocky Full (F) control with following command (type at Windows command prompt):例如,使用以下命令授予 Rocky Full (F) 控制权(在 Windows 命令提示符下键入):

C:> CACLS files /e /p rocky:f C:> CACLS 文件 /e /p Rocky:f

Read complete help by typing following command:通过键入以下命令阅读完整的帮助:

C:> cacls /? C:> cacls /?

I try the below way and it work for me:我尝试以下方法,它对我有用:
1. open cmd.exe 1.打开cmd.exe
2. takeown /R /F *.* 2. takeown /R /F *.*
3. icacls * /T /grant [username]:(D) 3. icacls * /T /grant [username]:(D)
4. del *.* /S /Q 4. del *.* /S /Q

So that the files can become my own access and it assign to "Delete" and then I can delete the files and folders.这样文件就可以成为我自己的访问权限并将其分配给“删除”,然后我可以删除文件和文件夹。

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:如果您被损坏的权限设置锁定在文件夹之外,以下(可能)是Windows 7的完美解决方案:

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

For Windows 10 the user/SID must be specified after the /remove:d option:对于Windows 10 ,必须在/remove:d选项之后指定用户/SID:

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

. .
Notes :注意事项

  1. The command is applied to the specified directory.该命令应用于指定的目录。

  2. Specifying the user "Everyone" sets the widest possible permission, as it includes every possible user.指定用户“Everyone”会设置尽可能广泛的权限,因为它包括所有可能的用户。

  3. 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.选项“/remove:d”删除可能存在的任何显式拒绝设置,因为这些设置会覆盖显式 ALLOW 设置:创建新 ALLOW 设置的必要准备。 This is only a precaution, as there is often no DENY setting present, but better safe than sorry.这只是一种预防措施,因为通常不存在拒绝设置,但安全总比抱歉好。

  4. The option "/grant" creates a new ALLOW setting, an explicit permission that replaces (":r") any and all explicit ALLOW settings that may exist.选项“/grant”创建了一个新的 ALLOW 设置,一个显式权限,用于替换 (":r") 任何和所有可能存在的显式 ALLOW 设置。

  5. The "F" parameter (ie the permission created) makes this a grant of FULL control. “F”参数(即创建的权限)使其成为完全控制的授予。

  6. The "/T" parameter adds recursion, applying these changes to all current sub-objects in the specified directory (ie files and subfolders), as well as the folder itself. “/T”参数添加递归,将这些更改应用于指定目录(即文件和子文件夹)中的所有当前子对象,以及文件夹本身。

  7. The "(OI)" and "(CI)" parameters also add recursion, applying these changes to sub-objects created subsequently. "(OI)" 和 "(CI)" 参数还添加了递归,将这些更改应用于随后创建的子对象。
    . .

ADDENDUM (2019/02/10) -附录 (2019/02/10) -

The Windows 10 command line above was kindly suggested to me today, so here it is.今天向我建议了上面的 Windows 10 命令行,所以它在这里。 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).我还没有 Windows 10 来测试它,但如果你有的话,请尝试一下(然后你可以在下面发表评论)。

The change only concerns removing the DENY setting as a first step.更改仅涉及删除 DENY 设置作为第一步。 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!我的理解是,在 Windows 7 上,您不需要在/remove:d之后指定用户,但我可能错了!

. .

ADDENDUM (2019/11/21) -附录 (2019/11/21) -

User astark recommends replacing Everyone with the term *S-1-1-0 in order for the command to be language independent.用户astark建议用术语 *S-1-1-0 替换每个人,以便命令与语言无关。 I only have an English install of Windows, so I can't test this proposal, but it seems reasonable.我只有英文版的 Windows,所以我无法测试这个建议,但它似乎是合理的。

I struggled with this for a while and only combining the answers in this thread worked for me (on Windows 10):我为此苦苦挣扎了一段时间,仅结合此线程中的答案对我有用(在 Windows 10 上):
1. Open cmd or PowerShell and go to the folder with files 1.打开cmd或PowerShell,进入文件所在文件夹
2. takeown /R /F . 2. 拿走 /R /F
3. icacls * /T /grant dan:F 3. icacls * /T /grant dan:F

Good luck!祝你好运!

With an Excel vba script to provision and create accounts.使用 Excel vba 脚本来配置和创建帐户。 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.我需要向我们的新用户授予使用我们的管理员“x”帐户创建的文件夹和子文件夹的完全权限。

cacls looked something like this: cacls \\FileServer\\Users\\Username /e /g Domain\\Username:C cacls 看起来像这样: cacls \\FileServer\\Users\\Username /e /g Domain\\Username:C

I needed to migrate this code to Windows 7 and beyond.我需要将此代码迁移到 Windows 7 及更高版本。 My solution turned out to be:我的解决方案原来是:

icacls \\FileServer\\Users\\Username /grant:r Domain\\Username:(OI)(CI)F /t icacls \\FileServer\\Users\\Username /grant:r Domain\\Username:(OI)(CI)F /t

/grant:r - Grants specified user access rights. /grant:r - 授予指定的用户访问权限。 Permissions replace previously granted explicit permissions.权限取代了先前授予的显式权限。 Without :r, permissions are added to any previously granted explicit permissions没有 :r,权限将添加到任何先前授予的显式权限

(OI)(CI) - This folder, subfolders, and files. (OI)(CI) - 此文件夹、子文件夹和文件。

F - Full Access F - 完全访问

/t - Traverse all subfolders to match files/directories. /t - 遍历所有子文件夹以匹配文件/目录。

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.以及创建新文件夹。

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:以防万一有人在此页面上绊倒,如果您想在一个命令中将各种权限串在一起,我使用了这个:

icacls "c:\TestFolder" /grant:r Test_User:(OI)(CI)(RC,RD,RX)

Note the csv string for the various permissions.请注意各种权限的 csv 字符串。

XCACLS.VBS is a very powerful script that will change/edit ACL info. XCACLS.VBS 是一个非常强大的脚本,可以更改/编辑 ACL 信息。 c:\\windows\\system32\\cscript.exe xcacls.vbs help returns all switches and options. c:\\windows\\system32\\cscript.exe xcacls.vbs 帮助返回所有开关和选项。

You can get official distribution from Microsoft Support Page您可以从Microsoft 支持页面获得官方发行版

Bulk folder creation and grant permission works me by using the below powershell script.通过使用以下 powershell 脚本,批量文件夹创建和授予权限对我有用。

Import-Csv "D:\Scripts\foldernames.csv" | foreach-object {
    $username = $_.foldername 

    # foldername is the header of csv file

    $domain = “example.com”

    $folder= "D:\Users"

    $domainusername = $domain+“\”+$username

    New-Item $folder\$username –Type Directory

    Get-Acl $folder\$username  

    $acl = Get-Acl $folder\$username

    $acl.SetAccessRuleProtection($True, $False)

    $rule = New-Object System.Security.AccessControl.FileSystemAccessRule("Administrators","FullControl", "ContainerInherit, ObjectInherit", "None", "Allow")
    $acl.AddAccessRule($rule)

    $rule = New-Object System.Security.AccessControl.FileSystemAccessRule("SYSTEM","FullControl", "ContainerInherit, ObjectInherit", "None", "Allow")
    $acl.AddAccessRule($rule)

    $rule = New-Object System.Security.AccessControl.FileSystemAccessRule("$domain\Domain Admins","Read", "ContainerInherit, ObjectInherit", "None", "Allow")
    $acl.AddAccessRule($rule)

    $rule = New-Object System.Security.AccessControl.FileSystemAccessRule($domainusername,"Modify", "ContainerInherit, ObjectInherit", "None", "Allow")
    $acl.AddAccessRule($rule)

    Set-Acl $folder\$username $acl
}

Note: You have to create same domain username in csv file otherwise you will get permission issues注意:您必须在 csv 文件中创建相同的域用户名,否则您会遇到权限问题

excellent point Călin Darie优秀的点 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.我有很多脚本可以使用 cacls 我将它们移动到 icacls 但是我找不到一个脚本来更改根安装卷示例: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. 1 获取文件夹挂载 GUID 到临时文件,然后读取 GUID 以将卷挂载为临时驱动器 X:应用秒并记录更改,然后仅从 X: 驱动器卸载卷,以便安装的文件夹不会被更改或中断其他然后是应用秒。

here is sample of my script:这是我的脚本示例:

**mountvol "d:\%1" /L >tempDrive.temp && FOR /f "tokens=*" %%I IN (tempDrive.temp) DO mountvol X: %%I 
D:\tools\security\icacls.exe  %~2 /grant domain\group:(OI)(CI)F /T /C >>%~1LUNsec-%TDWEEK%-%TMONTH%-%TDAY%-%TYEAR%-%THOUR%-%TMINUTE%-%TAM%.txt
if exist x:\*.* mountvol X: /d**

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.执行icacls "D:\\test" /grant John:(OI)(CI)F /T命令不起作用,因为它似乎没有从此列表中删除我名字中的“拒绝”。

The only thing that worked for me is resetting all permissions with the icacls "D:\\test" /reset /T command.唯一对我icacls "D:\\test" /reset /T是使用icacls "D:\\test" /reset /T命令重置所有权限。

  1. navigate to top level directory you want to set permissions to with explorer导航到要使用资源管理器设置权限的顶级目录
  2. type cmd in the address bar of your explorer window在资源管理器窗口的地址栏中键入 cmd
  3. enter icacls . /grant John:(OI)(CI)F /T输入icacls . /grant John:(OI)(CI)F /T icacls . /grant John:(OI)(CI)F /T where John is the username icacls . /grant John:(OI)(CI)F /T其中 John 是用户名
  4. profit利润

Just adding this because it seemed supremely easy this way and others may profit - all credit goes to Călin Darie .只是添加这个,因为这样看起来非常简单,其他人可能会获利 - 所有功劳都归功于Călin Darie

When I ran the command:当我运行命令时:

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

None of the files in folderB were being processed, which was indicated via the output message:没有处理文件夹 B 中的任何文件,这通过folderB消息指示:

Successfully processed 0 files; Failed processing 0 files

However, once I changed the specified path to the parent directory( "c:/path/to/folderA" ) and re-ran the command all the files in folderB were successfully processed.但是,一旦我将指定路径更改为父目录( "c:/path/to/folderA" )并重新运行该命令, folderB中的所有文件都已成功处理。

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

Hope this helps anyone running into the same issue.希望这可以帮助遇到同样问题的任何人。

i was not able to open any file in a drive, this command unlocked all -我无法打开驱动器中的任何文件,这个命令解锁了所有 -

icacls i:\* /grant Users:F /t /q /c
attrib +r +a +s +h <folder name> <file name> to hide
attrib -r -a -s -h <folder name> <file name> to unhide

in windows 10 working without "c:>" and ">"在没有“c:>”和“>”的Windows 10中工作

For example:例如:

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

cacls "file or folder path" /e /p UserName:F cacls "文件或文件夹路径" /e /p 用户名:F

(also this fixes error 2502 and 2503) (这也修复了错误 2502 和 2503)

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

This is what worked for me:这对我有用:

  1. Manually open the folder for which the access is denied.手动打开拒绝访问的文件夹。

  2. Select the Executable/application file in that folder.选择该文件夹中的可执行文件/应用程序文件。

  3. Right-click on it and go to Properties -> Compatibility右键单击它并转到“属性” ->“兼容性”

  4. Now see the Privilege Level and check it for Run As Administrator现在查看权限级别并检查以管理员身份运行

  5. Click on Change Settings for all users .单击更改所有用户的设置

The problem is solved now.现在问题解决了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Windows中使用命令行为所有用户删除目录和子目录的权限? - How to remove permission to all users for a directory and subdirectory using command line in windows? 如何使用 Windows 命令行更改目录 - how to change directory using Windows command line 如何使用特定用户的命令行查看 Windows 中的文件夹权限? - How to view folder permission in windows using command line for particular user? 从 Inno Setup 向 SQL Server 授予文件夹权限的 Windows 命令 - Windows command to grant folder permission to SQL Server from Inno Setup 如何从 Windows 上的命令行查看命名管道权限 - How to see named pipe permission from command line on windows 如何以编程方式获取 Windows 命令行或 Windows 资源管理器的当前目录? - How to programmatically get current directory of a Windows command line or Windows explorer? 如何以域管理员身份从命令行注销 Windows 上的所有用户 - How to logoff all users on windows from command line as a domain administrator 如何使用批处理从Windows命令行中的每个子目录返回最新文件 - How to return the newest file from each sub-directory in Windows command line using batch 使用Windows命令行在每个目录下创建子目录 - Create Subdirectories Under Each Directory Using Windows Command Line 使用 Windows 命令行扁平化多个根目录的目录层次结构 - flatten directory hierarchy for multiple root directories using Windows command line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM