Search code examples
windowspowershellinheritanceacl

Remove inherited Access rules from folder/file trough powershell


I have a problem with removing inherited rules on my folder. When I add or remove new rules (e.g. Write is inherited, I add Read) I have no problem with removing them, but I want to get rid of all the rules (to remove even user). I use this function, and it is working fine, but does not remove inherited rules.

Function deleteAllRightsForUser{
    $ar = New-Object System.Security.AccessControl.FileSystemAccessRule($username,$right, $InheritanceFlag, $PropagationFlag, $accessControl) 
    $acl.RemoveAccessRuleAll($ar) 
    Set-Acl $folder $acl
}

Solution

  • You need to protect the ACL from inheritance.

    The first argument is that you wish to enable it (disable inheritance), the second is that you wish to drop the entries rather than copying them to the updated ACL.

    $acl.SetAccessRuleProtection($true, $false)