Remove Access
This PowerShell script block performs the following actions:
- Sets the
$Path
variable to"C:\MyFolder"
. - Gets the current Access Control List (ACL) for the folder at
$Path
using theGet-Acl
cmdlet and stores it in the$ACL
variable. - Searches for an access control entry (ACE) in the ACL where the
IdentityReference
property is equal to"User1"
. This is done using theWhere-Object
cmdlet and theAccess
property of the$ACL
variable. The result is stored in the$ACE
variable. - Creates a new ACE for
"User1"
with the permission of “ReadAndExecute” and theAccessControlType
of “Deny”. This is stored in the$NewACE
variable. - The
$ACL
variable is updated with the new ACE using theSetAccessRule
method. - The old ACE is removed from the
$ACL
variable using theRemoveAccessRule
method. - Finally, the
$ACL
variable with the updated permissions is applied to the folder at$Path
using theSet-Acl
cmdlet.
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