Generally speaking, a combination of Get-Acl
and Set-Acl
should be able to accomplish what you need. However, Get-Acl has an annoying limitation that can manifest as being unable to write the modified ACL back using Set-Acl due to insufficient permissions (unless you have rights to also change ownership). More info on that problem can be found in this SO question.
In any case, for filesystem permissions you can work around Get-Acl's limitation by using a method from the object returned by Get-Item
instead.
$acl = (Get-Item C:\myfolder).GetAccessControl('Access')
If you examine the $acl.Access
property of the returned object, you'll find that it's a collection of FileSystemAccessRule
objects (a.k.a. ACE objects). Ultimately, you want to find the subset of those ACEs that match the user you're trying to remove and also ignore any that are inherited. You can't actually remove inherited ACEs and even Windows Explorer will tell you as much if you try and remove them using the GUI. In any case, here's how you might get that subset of ACEs.
$acesToRemove = $acl.Access | ?{ $_.IsInherited -eq $false -and $_.IdentityReference -eq 'MYCOMPUTER\myuser' }
Now that you have the ACEs to remove, you just need to remove them from your original ACL and write it back to the folder.
$acl.RemoveAccessRuleAll($acesToRemove)
Set-Acl -AclObject $acl C:\myfolder\