Remove Access
The given PowerShell code performs the following actions:
- Sets the variable
$Pathto the path of the folder for which a specific access rule needs to be removed. - Gets the current ACL (access control list) of the folder using the
Get-Aclcmdlet and assigns it to the variable$ACL. - Finds the access control entry (ACE) in the
$ACLobject where theIdentityReferenceproperty is equal to"User1". The resulting ACE is assigned to the variable$ACE. - Removes the ACE from the
$ACLobject using theRemoveAccessRule()method. - Sets the modified ACL to the folder using the
Set-Aclcmdlet.
In summary, the code removes a specific access rule (represented by the ACE) from the folder’s ACL, thereby revoking the corresponding permissions for the specified user or group.
$Path = "C:\MyFolder"
$ACL = Get-Acl $Path
$ACE = $ACL.Access | Where-Object {$_.IdentityReference -eq "User1"}
$ACL.RemoveAccessRule($ACE)
Set-Acl $Path $ACL