How to remove users with Get-WMIObject & Remove-WmiObject

Good afternoon good people,

I’m after creating a small script to delete user profiles daily (both their regedit entries and folder so that it is a “clean” delete).

When I simply do “Remove-WmiObject -class Win32_UserProfile” in my test environment well… it wipes “it”.

I suspect I can use get-WMIObject to set a where “this conditions” similar to SQL but I have just started with my PowerShell so I’m a bit lost as to how achieve this?

I’m after deleting all users daily upon reboot (For an RD server). There’s a GPO that can achieve this with profiles older than 1 day, but I want to delete profiles regardless of anything daily.

Many thanks!

 

 

 

 

Please do not invent the wheel again and again … use your favorite internet search engine first before you start to build something what already exists. :wink:

https://adamtheautomator.com/powershell-delete-user-profile/

BTW: Why do you actually want to do that? That’s a kind of very weird requirement.

[quote quote=287482]Please do not invent the wheel again and again … use your favorite internet search engine first before you start to build something what already exists. 😉

https://adamtheautomator.com/powershell-delete-user-profile/

BTW: Why do you actually want to do that? That’s a kind of very weird requirement.

[/quote]

Thanks I’ll give it a good read.

I just like to try and figure it out myself to learn along, there are scripts out there for everything so just with the basics you can kind of get along with what you find for your needs and never really learn/progress. So I have switched to a an attitude of try first (when I have the luxury of time, that is) and learn.

There’s a Spanish saying “una vez visto todo el mundo es listo” meaning “once seen, everyone is smart”. I just tend to learn when I have to sweat a bit for the answer, so I give it a go when I think I have a chance to actually figure it out from scratch.

At any rate, I have some temporary RD servers that aren’t going to stick around for long. I have been denied to configure roaming profiles on them, so this is the best way to keep the C drive free of user clutter.

Ok, but this way you force your users to configure their initial settings on every single server again and again and again. I’d consider that as non-sustainable waste of time and energy.

Other than the “extra” time of that first login everything they need is pre-configured (with the exception of browser favourites).