Im trying to remove a group from a bunch of subfolders permission using powershell, but for some reason it doesn't apply to the subfolder

Viewed 38

Im trying to remove a group from a bunch of subfolders permission using powershell, but for some reason it doesnt apply to the subfolder, it oly runs and apply the changes to the main folder, here is the code. Can someone help?

$grupo = "PAULWURTH\Domain Users"
$dir = "C:\Robocopy\TESTE\userdata$"

$subfolder=Get-ChildItem -Path $dir -Directory -Recurse
foreach ($subfolder in $dir){               
# Disable inherit
$acl = Get-ACL -Path $subfolder
$acl.SetAccessRuleProtection($True, $True)
Set-Acl -Path $subfolder -AclObject $acl
# Removes group permission
$acl = Get-Item $subfolder |get-acl
$acl.Access |where {$_.IdentityReference -eq $grupo} |%{$acl.RemoveAccessRule($_)}
$acl |Set-Acl
}
1 Answers

I believe it is how you are using the variables $subfolder and $dir the wrong way in the foreach statement.

$grupo = "PAULWURTH\Domain Users"
$dir = "C:\Robocopy\TESTE\userdata$"

$allSubFolders=Get-ChildItem -Path $dir -Directory -Recurse

foreach ($subfolder in $allSubFolders){               
# Disable inherit
$acl = Get-ACL -Path $subfolder
$acl.SetAccessRuleProtection($True, $True)
Set-Acl -Path $subfolder -AclObject $acl
# Removes group permission
$acl = Get-Item $subfolder |get-acl
$acl.Access |where {$_.IdentityReference -eq $grupo} |%{$acl.RemoveAccessRule($_)}
$acl |Set-Acl
}
Related