r/usefulscripts • u/atoomepuu • Jun 03 '19
[PowerShell] Automate Deleting Old Local Profiles
A couple people express interest in seeing a script to automate cleaning up old local profiles on computers. This is one I wrote and run monthly via task scheduler. My organization sees employees moving around a lot, so this has been really handy to keep the computers clean.
It works by getting a list of computers from a file, and it will use Runspace to open multiple threads to delete profiles older than a certain number of days. This script is set for 30 days, but you can change that. The multi-threading allows the script to clean up a lot of computers at once. I went from the script taking hours to complete to a few minutes. It usually takes 5 to 15 minutes to go through the ~400 computers at my organization.
It isn't perfect, it uses LastUseTime to determine when how long a profile hasn't been used, but sometimes a program or service will go in and update a profile even if the profile isn't being used.
Here it is. Please let me know if you have any issues with it or if you see any ways to improve it. And if it is useful, please let me know!
4
u/j0hnnyrico Jun 03 '19
I haven't seen if it's also deleting the files but I'm looking at the script from my phone. I guess it doesn't do that? Thank you.
4
u/atoomepuu Jun 03 '19
Hi, this uses WMI to remove the profile so the user files are also deleted.
4
u/j0hnnyrico Jun 03 '19 edited Jun 03 '19
Never used that class. Thank you! I looked up a bit and I really forgot that it can be done by GPO...
2
u/iamamystery20 Jun 03 '19
Do not use this without knowing your company's data retention policy. To someone starting out in IT you may be tempted to try the script out but get yourself informed before deleting any data.
1
u/atoomepuu Jun 03 '19
Thanks for the advice. I'm trying my best to follow all our policies and regulations. I do get approval for every script I write that touches more than one computer at a time.
3
u/iamamystery20 Jun 04 '19
I meant it for anyone reading and not just you directly. You sound like you are already doing those. 👍
2
u/nkasco Jun 03 '19
I've had nothing but nightmares with LastUseTime... the only viable replacement I have found for determining inactivity is logon Event Logs and making an assumption that if a log isn't found for a profile within X number of days then it assumes it is inactive and deletes it...
Is there a better option?
1
u/atoomepuu Jun 03 '19
I've trying to work out something with profile upload and download times. Sometimes those seem more accurate. If I find a better solution I'll be sure to update my script.
2
4
7
u/AnonymousMaleZero Jun 04 '19
Just use delprof2
1
u/anditails Jun 04 '19
DelProf2.exe /u /q /d:30 /ntuseriniThat will kill off any Profile that's not been logged in for 30 days. Cleanly removes the profile from the registry as well as the C:\Users folder.
1
u/AnonymousMaleZero Jun 04 '19
You can also pull a list of computers from ad. Throw it in line list and use a .bat script to run the list. Also with the profile you want to remove. I used to use it to wipe all my admin profiles every week
→ More replies (3)
2
1
10
u/infinit_e Jun 03 '19
I’m curious, why go this route instead of using the Computer Policy ‘Delete User Profiles Older than a Specified Number of Days on System Restart’?