Created
September 14, 2016 14:41
-
-
Save terrytrent/7fc2fd361dbac774850fbe5f3840c077 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$PathToHomeFolder = \\domain.com\home$ | |
$FoldersToFix = Get-Childitem $PathToHomeFolder | |
$GroupOrUserToRemove = 'Domain Users' | |
foreach($folder in $FoldersToFix) | |
{ | |
$acl = get-acl $folder | |
$aclObjectsToRemove = $acl.access.where({$_.IdentityReference -like "*$GroupOrUserToRemove*"}) | |
if($aclObjectsToRemove.count -gt 0) | |
{ | |
foreach($object in $aclObjectsToRemove) | |
{ | |
Write-Host "Attempting to remove ACL Access for folder: $folder`n Identity: $($object.IdentityReference)`n Rights: $($object.FileSystemRights)" -ForegroundColor Yellow | |
$aclToRemove = new-object System.Security.AccessControl.FileSystemAccessRule($object.IdentityReference,$object.FileSystemRights,$object.InheritanceFlags,$object.PropagationFlags,$object.AccessControlType) | |
try | |
{ | |
$acl.RemoveAccessRule($object) | out-null | |
Write-Host "Success" -ForegroundColor Green | |
} | |
catch | |
{ | |
Write-Host "There was a problem removing the ACL Access" -ForegroundColor Red | |
} | |
} | |
} | |
else | |
{ | |
Write-Host "No matching ACL found for $folder" -ForegroundColor Cyan | |
break | |
} | |
try | |
{ | |
Write-Host "Attempting to save ACL modification to folder $folder.`nThis may take some time if inheritance is enabled and there are a large number of files." -ForegroundColor Yellow | |
Set-Acl -Path $folder -AclObject $acl | |
Write-Host "Success" -ForegroundColor Green | |
} | |
catch | |
{ | |
Write-Host "There was a problem saving the ACL modification" -ForegroundColor Red | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment