Delete local user profiles using powershell – the quick way.

I needed a script to be able to delete local user profiles on computers and all of the existing scripts I found on the Internet were either overly complicated or would only delete the directory whilst leaving the registry entries in place.

Other scripts I found would run synchronously when you had multiple computers ie one at a time…you would have to sit and watch it plod along whilst 100 or so profiles were slowly deleted one at a time before moving on to the next computer and doing the whole thing again. Not fun when you have 50 or 60 computers to get through.

Although I considered using a workflow for this, (as I needed to run this on lots of computers at once), I found that it overcomplicated things when really I only needed to utilise Powershell jobs which did the – and no pun intended – job.  This is a great Powershell feature and in essence, by using -AsJob I’ve achieved what I would be looking to do with a workflow but without the complexity that workflows bring: Rather than wait for  computer1 to delete the profiles then computer2 then computer3…etc, I have sent the ‘job’ to all computers in the pipeline at once so all computers are running the script at the same time.

The script itself is nothing flash, it simply does what it needs to do – no user menu, simply fire and forget – which met my requirements. (Although maybe not yours – you can always embellish the script for your own purposes if you need more from it.)

Special accounts and any logged on user will be ignored and won’t be deleted – this is great as it means you can run it on machines that are already logged on.  If you have any other accounts that you want to keep, simply add them to the filter in the code – that part is pretty straight forward.

It also accepts pipeline input so if you want to pipe in a text file of computer names or whatever then go ahead.

You can find the script in my Powershell repo on github and the script is called: Delete-Profiles

Be the first to comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.