Hello everyone,<\/p>\n
I am facing an issue where there is a need to remove a BULK of old files and folders.<\/p>\n
The problem is, in the entire folder structure there are several permission inheritances broken down the line, not only on folders but also on files. To make it more complex, I’m also not the owner of these files and folders so it’s nearly impossible to delete them.<\/p>\n
I do get the folder permissions right, but because the inheritance is also broken on FILE level, I don’t seem to get it working.<\/p>\n
Here is the code I have written which seems to work on the folders, but not on the files.<\/p>\n
I use a drive mapping, because the folders go quite deep and exceed the 260 characters max file path length.<\/p>\n
My apologies for the cryptic script, I’m fairly new to powershell and this forum.<\/p>\n
#Declare folder and path location to delete\n\n $folders = Import-Csv -Path C:\\temp\\remove_folders_test.csv\n $path = *servername*\n\n#Declare ACL variables\n\n $rights = 'FullControl'\n $inheritance = 'ContainerInherit, ObjectInherit'\n $identity = 'domainname\\username'\n $propagation = 'None'\n $type = 'Allow'\n\n $owner = new-object System.Security.Principal.NTAccount(\"domain\\username\")\n\n $Permissions = New-Object System.Security.AccessControl.FileSystemAccessRule($identity,$rights,$inheritance,$propagation,$type)\n\n#Enable permission inheritance on folder, subfolders & files\n\n$folders | foreach {\n \n $fullpath = $path + \"\\\" + $_.Foldername\n\n \n\n $acl = Get-Acl -Path $fullpath\n $acl.SetOwner($owner)\n $acl.AddAccessRule($Permissions)\n $acl.SetAccessRuleProtection($false, $true)\n\n Net Use /d K:\n Net use K: $fullpath\n write-host $fullpath\n\n $subfolder = Get-ChildItem -LiteralPath \"K:\" -Recurse -Force | Set-Acl -AclObject $acl | icacls.exe $subfolder /t /reset\n\n Remove-Item -LiteralPath $fullpath -Recurse -Force\n\n}\n<\/code><\/pre>\nAny help would be very much apreciated!<\/p>","upvoteCount":2,"answerCount":7,"datePublished":"2021-04-14T06:15:16.000Z","author":{"@type":"Person","name":"spiceuser-pxb8e","url":"https://community.spiceworks.com/u/spiceuser-pxb8e"},"suggestedAnswer":[{"@type":"Answer","text":"
Hello everyone,<\/p>\n
I am facing an issue where there is a need to remove a BULK of old files and folders.<\/p>\n
The problem is, in the entire folder structure there are several permission inheritances broken down the line, not only on folders but also on files. To make it more complex, I’m also not the owner of these files and folders so it’s nearly impossible to delete them.<\/p>\n
I do get the folder permissions right, but because the inheritance is also broken on FILE level, I don’t seem to get it working.<\/p>\n
Here is the code I have written which seems to work on the folders, but not on the files.<\/p>\n
I use a drive mapping, because the folders go quite deep and exceed the 260 characters max file path length.<\/p>\n
My apologies for the cryptic script, I’m fairly new to powershell and this forum.<\/p>\n
#Declare folder and path location to delete\n\n $folders = Import-Csv -Path C:\\temp\\remove_folders_test.csv\n $path = *servername*\n\n#Declare ACL variables\n\n $rights = 'FullControl'\n $inheritance = 'ContainerInherit, ObjectInherit'\n $identity = 'domainname\\username'\n $propagation = 'None'\n $type = 'Allow'\n\n $owner = new-object System.Security.Principal.NTAccount(\"domain\\username\")\n\n $Permissions = New-Object System.Security.AccessControl.FileSystemAccessRule($identity,$rights,$inheritance,$propagation,$type)\n\n#Enable permission inheritance on folder, subfolders & files\n\n$folders | foreach {\n \n $fullpath = $path + \"\\\" + $_.Foldername\n\n \n\n $acl = Get-Acl -Path $fullpath\n $acl.SetOwner($owner)\n $acl.AddAccessRule($Permissions)\n $acl.SetAccessRuleProtection($false, $true)\n\n Net Use /d K:\n Net use K: $fullpath\n write-host $fullpath\n\n $subfolder = Get-ChildItem -LiteralPath \"K:\" -Recurse -Force | Set-Acl -AclObject $acl | icacls.exe $subfolder /t /reset\n\n Remove-Item -LiteralPath $fullpath -Recurse -Force\n\n}\n<\/code><\/pre>\nAny help would be very much apreciated!<\/p>","upvoteCount":2,"datePublished":"2021-04-14T06:15:16.000Z","url":"https://community.spiceworks.com/t/broken-file-permissions/796774/1","author":{"@type":"Person","name":"spiceuser-pxb8e","url":"https://community.spiceworks.com/u/spiceuser-pxb8e"}},{"@type":"Answer","text":"
If you post code, please use the ‘Insert Code’ button. Please and thank you!<\/p>\n