Copy security permissions of one file to another

19

11

I would like to replicate the permissions of a file to another. I do not want to copy the data, only the permissions.

For example, I created a new file and want it to have the same permissions as another file that already existed.

TaoTao

Posted 2013-09-13T21:21:46.417

Reputation: 293

Same permissions as another file, but different than the directory they are in? – PsychoData – 2013-09-13T21:28:47.207

Answers

28

You can do it in a few steps with icacls and a text editor.

First you must save the permissions of the original file

icacls C:\test\file1.bin /save perms.txt

you will need to edit the perms.txt file you just created in whatever folder you ran icacls in. The file should look something like this:

file1.bin
D:AI(A;;0x1301bf;;;BU)(A;ID;FA;;;BA)(A;ID;FA;;;SY)(A;ID;0x1200a9;;;BU)(A;ID;0x1301bf;;;AU)

You can ignore all the stuff on the 2nd row, all we care about is the first row. Change the file1.bin to your new filename file2.bin and save the file.

Now you just need to restore the file permissions on the 2nd file, note that we did not include the filename this time. (If you get an error that says "Not all privileges or groups referenced are assigned to the caller." run the program again in an elevated command prompt.)

icacls C:\test\ /restore perms.txt

Scott Chamberlain

Posted 2013-09-13T21:21:46.417

Reputation: 28 923

maybe put these into a script to somewhat automate the task? – PsychoData – 2013-09-13T21:36:33.890

2It could be done, but I can't think off of the top of my head how to do step 2 easyly. Anyway if I was going to script it, I would just do the entire thing in Powershell using File.GetAccessControl() and File.SetAccessControl() – Scott Chamberlain – 2013-09-13T21:38:33.317

4

The easiest way, by far, is to use PowerShell and run:

Get-Acl .\file1 | Set-Acl .\file2

Giovanni Bassi

Posted 2013-09-13T21:21:46.417

Reputation: 141

4

To make it in a batch (copy security pemissions from a subfolder or all folders of one drive to the same folder/file stricture on another drive), create a, say, cpdacls.bat file with this content:

@echo off
for /r %1 %%f in (.) do call :icacls_one %%f %2

goto :eof

:icacls_one

icacls %1\*.* /save %TEMP%\perms.txt
icacls %2.%~pnx1 /restore %TEMP%\perms.txt
del /q %TEMP%\perms.txt

And then run it like cpdacls.bat e:\ f:\.

Note that the folder structure in the destination folder must be the same as folder structure in the source folder, taken from its root, not depending on what subfolder you have specified as a source folder.

Olexa

Posted 2013-09-13T21:21:46.417

Reputation: 199