I have a very large list of folders and I need to remove a single ACL from each of them. Rather than doing it manually, I'm trying to write a script to do it in a fraction of the time, but I'm running into a bit of trouble.
Here is what I have so far:
$filepath = "C:\ALCTEST"
$user = "domain\username"
$folders = @((get-item $filePath))
$folders += Get-ChildItem $filePath -Recurse |
where { $_.PSIsContainer -ne $false }
##Need to do this in order to remove item 0 from the array, otherwise
##item 0 is the parent folder
$newfolders = $folders[1,2 + 3..($folders.length - 1)]
foreach ($folder in $newfolders) {
$acl = Get-Acl -Path $folder.FullName
foreach ($access in $acl.access) {
foreach ($value in $access.IdentityReference.Value) {
if ($value -eq $user) {
$acl.RemoveAccessRule($access) | Out-Null
}
}
}
Set-Acl -Path $folder -AclObject $acl
}
From what I watched during the debug, I think everything works right up until I try to set the ACL back onto the folders. When it gets to that line, I get the error
Cannot find path 'C:\Windows\system32\01' because it does not exist.
Inside the parent folder of ACLTEST are seven folders named "01"..."07" with various ACLs for testing.
I'm not sure where to go from here. I was reading this Scripting Guy article which helped me through a lot, but his script seems to be focused on manually entering folder paths, which I definitely can't do for the several hundred folders that need to be changed.
Any help is appreciated. I'm new to PowerShell scripting, so if you see any mistakes other than what I'm referring to in the script above, I'd love to hear them.