Remove Special NTFS permission from folder and subfolders

215 Views Asked by At

Below code which I have for removing special NTFS permission

Foreach($folder in $path) { 
 icacls $folder /inheritance:d
 Get-ChildItem -Path $folder -Recurse | ?{$_.PSisContainer} | foreach {$subfolder = $_.FullName; icacls $subfolder /inheritance:d}[![enter image description here][1]][1]
}

# Check the existing rights
$acl.Access | where IdentityReference -Like 'BUILTIN\Users'

# Get a list of the rules to remove
$rules = $acl.access | Where-Object { 
    !$_.IsInherited -and 
    $_.IdentityReference -like 'BUILTIN\Users' -and
    $_.FileSystemRights -in 'CreateFiles, AppendData'
}

# Remove those rules from the ACL object 
ForEach($rule in $rules) {
    $acl.RemoveAccessRule($rule)
}

# Check that the remaining rules look good:
$recheckpermissions = $acl.Access

# Finally, set the ACL

Set-Acl -Path $path -AclObject $acl

This code is working fine for the ROOT folder (e.g. in my case C:\IBM), but it not removing the same for the subfolder under this. Please let me know what is issue here

Root Folder

enter image description here

SubFolder

enter image description here

0

There are 0 best solutions below