Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Delete user profiles except admin

$
0
0

I work as an IT assistant in higher education and often have to add/remove software, printers, etc… to classroom computers. When doing this, we have to delete all the user profiles from the local machine except out domain administrator, local administrator, and Default user. As you know, this can be tedious to say the least. Unfortunately, we do not have remoting enabled for powershell, and computers only have Version 2 installed. Instead we can write scripts and push via LANDesk, which is useful but often times takes much longer. So here is what I got so far. I am new to Powershell, so I could be way off on this, but I am trying to write it as if I was writing it for the local computer.

$Users = Get-WmiObject -Class Win32_UserProfile
$IgnoreList = “DAdmin”, “LAdmin”, “Default”, “NetworkService”, “LocalService”, “SystemProfile
ForEach ($User in $Users) {
If ($User.localpath -notlike “*\$IgnoreList”) {
$User.delete()
}
}
It worked fine using only one name from the $IgnoreList, but once I added more it failed. I am guessing that I need a nested loop for each user to go through each name from the $IgnoreList, but I am not sure how.
Is the reason it isn’t working is because each profile is checking that all those names aren’t part of it instead of one at a time?
Thank you very much for any assistance you can offer.


Viewing all articles
Browse latest Browse all 13067

Trending Articles