How to use powershell and set-acl to replicate permissions across folder structure and for specific extensions?

There are a set of folders and subfolders in windows containing *.dxf (many), *.add (many) and shapes.dat (1 per folder) files. These are part of a folder structure required by a software.

I need to recursively set permisions for all *.dxf and *.add to read-only while leaving individual shapes.dat files with write permission.

In the follwing link something is described that seems to help, but I see no way to refactor no enable my operation.

The commands suggested are:

$newacl = get-acl "c:\yourfolder\samplefile.dxf"
get-childitem "C:\folder\folder\client files\" -Recurse -include "*.dxf" | set-acl -aclobject $newacl

I have also found a couple related questions: I've tried solving this with limited success (having to delete and restore from backup).

Any attempt will be appreciated.

1 Answer

I think you almost have it since I am a bit unsure what exactly is not working, you should to alter your Get-ChildItem statement. Based on your requirements:

I need to recursively set permisions for all *.dxf and *.add to read-only while leaving individual shapes.dat files with write permission.

Assuming that the file "samplefile.dxf" has the correct permissions, your code should look like this:

$newacl = Get-Acl "c:\yourfolder\samplefile.dxf"
Get-ChildItem -Path "C:\folder\folder\client files\" -Recurse -Include "*.dxf", "*.add" -Force | Set-Acl -AclObject $newacl

To point out the changes, I added -Path parameter before the target path and the -Force parameter, and added the additional "*.add" file extension. You should run this in an admin session of PowerShell to prevent permission errors, which might be the root issue. For more information on using multiple strings as filters with the Get-ChildItem command refer to this article. If this does not work, add the -Verbose parameter to the Set-Acl command to see what is failing.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like