Skip to content

Instantly share code, notes, and snippets.

@terrytrent
Created September 14, 2016 14:41
Show Gist options
  • Save terrytrent/7fc2fd361dbac774850fbe5f3840c077 to your computer and use it in GitHub Desktop.
Save terrytrent/7fc2fd361dbac774850fbe5f3840c077 to your computer and use it in GitHub Desktop.
$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