Recursively changing permissions with Powershell

12,046

Untested but how about something like this:

$acls = @{};
Get-ChildItem Template |
    Where-Object { $_.PSIsContainer } |
    ForEach-Object {
        $acls[$_.Name] = Get-Acl $_.FullName ;
    }

Get-ChildItem Folder2 -Recurse |
    Where-Object { $_.PSIsContainer -and $acls.ContainsKey($_.Name) } |
    ForEach-Object {
        Set-Acl $_.FullName $acls[$_.Name] ;
    }
Share:
12,046

Related videos on Youtube

Madhuri
Author by

Madhuri

Updated on September 17, 2022

Comments

  • Madhuri
    Madhuri almost 2 years

    I am trying to write a script in Powershell, that will find folders recursively and based on the folder name replace the ACL on the folder.

    I have folders already with the correct permissions. These I use as template folders.

    Folder1
    Folder2
     - Subfolder1
     - Subfolder2 
    

    There are literally thousands of folders, all with the same names, and I would like to apply the same set of permissions to all of the like named folders. I have worked on tackling this a few different ways but I haven't found a good way to handle the sub-folders and recursion.

    Here is a sample of the first way I tried to tackle this.

    $Prop = Get-Acl "Template\Subfolder1"
    $Engd = Get-Acl "Template\Subfolder2"
    
    foreach ($file in $(Get-ChildItem Folder2 -recurse)) {
      If ($_ = "Subfolder1") {
        set-acl $_ $Prop
        }
      If ($_ = "Subfolder2") {
        set-acl $_ $Engd
        }
    }
    

    The further I get along with trying to write this the more I realize its going to be harder and harder. I have hundreds of folders named similar to Folder2 Folder3..... FolderN, all with the same folder structure and folder names inside. I just want something simple that will match a folder name and based on the name assign a certain predefined ACL.

  • Madhuri
    Madhuri almost 15 years
    First I added -recurse after Template on the second line, because my folder structure is 3 lvls deep. Then I ran it. I get this error. Get-Acl : Cannot find path 'Subfolder 1' because it does not exist. At C:\scripts\aclt.ps1:5 char:33 + $acls[$_.Name] = Get-Acl <<<< $_ ; I think it is because when you use Get-Acl it does not use the full path of the current folder its working with. But I dont know.
  • Jason Stangroome
    Jason Stangroome almost 15 years
    If in your template folder Parent1 and Parent2 both have a subfolder called Child1 but with different ACLs then the script will need to be improved to check the parent hierarchy to ensure the correct ACL is applied.
  • Madhuri
    Madhuri almost 15 years
    Yea no. None of the subfolders in the template folder share the name name. By the way I get the same error for every folder in my template folder. The only thing that changes is the name of the folder it appears to be pulling the acl from. Then an error when it trys to apply it.
  • Madhuri
    Madhuri almost 15 years
    I modified it slightly. Added a write-host $_.FullName right before the $acls[$_.Name] = Get-Acl $_.FullName and it printed off the name of the current folder. Which seems correct, but when it tries to grab the ACL it doesnt seem to work.
  • Madhuri
    Madhuri almost 15 years
    Works like a charm, after changing it to Set-Acl $_.Fullname