Reputation: 23
I've been running into issues with deleting user profiles at the end of a school year. I do it by hand but I'm trying to automate as much as possible via Powershell. Admins have rights to all folders via GPO and folder settings, but I get a denied when I just try to Remove-Item, even when running Powershell as Administrator. As a way around that, I found the Get-ACL/SetOwner bit...but when I try to apply THAT, I'm limited by one of the main annoyances of doing it by hand. The TMP files or just sometimes kids name their files long names past the name limit.
So this is the script I'm running
$lab1= "C:\LabFolders\G1\G1Users"
$lab2= "C:\LabFolders\G2\G2Users"
$lab3= "C:\LabFolders\G3\G3Users"
$Group = New-Object System.Security.Principal.NTAccount("Builtin", "Administrators")
$ACL = Get-ACL $lab2
$ACL.SetOwner($Group)
Get-ChildItem $lab2 -Recurse -Force | % {
Set-ACL -AclObject $ACL -Path $_.fullname
}
Get-ChildItem -Path $lab1 -Recurse| Foreach-object {Remove-item -Recurse -path $_.FullName -Force }
Get-ChildItem -Path $lab2 -Recurse| Foreach-object {Remove-item -Recurse -path $_.FullName -Force }
Get-ChildItem -Path $lab3 -Recurse| Foreach-object {Remove-item -Recurse -path $_.FullName -Force }
And that's been working in general, but I get this message a lot:
Set-ACL : Cannot find path 'C:\LabFolders\G2\G2Users\G2-5021.V6\Downloads\what are the different layers of the atmosphere - Google Search_files\m
=syas,CnSW2d,sysv,sysx,sysy,syt0,sy11o,sy1u7,sy1ue,VD4Qme,sy20l,Dq2Yjb,sy1z0,qmdEUe,ND0kmf,sy20m,UqGwg,lLQWFe,dtl0hd,sy125,NVlnE,sy19e,Exk9Ld,GGT
Ogd,khkNpe' because it does not exist.
At line:12 char:5
+ Set-ACL -AclObject $ACL -Path $_.fullname
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\LabFolders\G...d,GGTOgd,khkNpe:String) [Set-Acl], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetAclCommand
Remove-item : Cannot find path 'C:\LabFolders\G2\G2Users\G2-5021.V6\Downloads\what are the different layers of the atmosphere - Google Search_fil
es\m=syas,CnSW2d,sysv,sysx,sysy,syt0,sy11o,sy1u7,sy1ue,VD4Qme,sy20l,Dq2Yjb,sy1z0,qmdEUe,ND0kmf,sy20m,UqGwg,lLQWFe,dtl0hd,sy125,NVlnE,sy19e,Exk9Ld
,GGTOgd,khkNpe' because it does not exist.
I can rename the user folder and a couple levels deep if I need to then I'm able to delete the folder itself...is there a way to automate that bit? It's erroring on the Set-ACL part and then, naturally, the delete part. But I wasn't able to delete without the Set-ACL.
I checked a few similar topics but they all are a lot more complex than our setup is and I wasn't sure how to adapt them.
Edit: I'm aware that it's only set up to do the $lab2 folder, I was testing the script. $lab1 was already empty. Though I'm not sure how well the script will work when I add multiple labs at once with the Set-ACL part. Likely I'll just need to add more variable names.
Upvotes: 1
Views: 79
Reputation: 39
cacls\icacls to set ACL. rmdir to remove files. robocopy,xcopy to copy files. Usually, these utilities better then PowerShell Cmdlet to handle long names.
My english is not very good.I use Google Translate to translate Chinese into English.hope this can help you.
Upvotes: 0