Remove Access
This PowerShell script block performs the following actions:
- Sets the
$Pathvariable to"C:\MyFolder". - Gets the current Access Control List (ACL) for the folder at
$Pathusing theGet-Aclcmdlet and stores it in the$ACLvariable. - Searches for an access control entry (ACE) in the ACL where the
IdentityReferenceproperty is equal to"User1". This is done using theWhere-Objectcmdlet and theAccessproperty of the$ACLvariable. The result is stored in the$ACEvariable. - Creates a new ACE for
"User1"with the permission of “ReadAndExecute” and theAccessControlTypeof “Deny”. This is stored in the$NewACEvariable. - The
$ACLvariable is updated with the new ACE using theSetAccessRulemethod. - The old ACE is removed from the
$ACLvariable using theRemoveAccessRulemethod. - Finally, the
$ACLvariable with the updated permissions is applied to the folder at$Pathusing theSet-Aclcmdlet.
In summary, this script block removes a specific access control rule ($ACE) for "User1" and replaces it with a new rule that denies "User1" the “ReadAndExecute” permission.
$Path = "C:\MyFolder"
$ACL = Get-Acl $Path
$ACE = $ACL.Access | Where-Object {$_.IdentityReference -eq "User1"}
$NewACE = New-Object System.Security.AccessControl.FileSystemAccessRule("User1","ReadAndExecute","Deny")
$ACL.SetAccessRule($NewACE)
$ACL.RemoveAccessRule($ACE)
Set-Acl $Path $ACL