Script to delete local profiles

Hey everyone, PS new guy here. I’m trying to come up with a script to run that will remove all basic profiles from a Windows 10 workstation. Here’s what I’ve come up with but I’m having zero luck. It returns to a prompt like it has ran, but the user profiles remain.

 

$ErrorActionPreference= 'silentlycontinue'
$ProfilesToDelete = Get-CimInstance -Class Win32_UserProfile -filter "special = false"

foreach ($User in $ProfilesToDelete) {
	$User.Delete()
}

 

First, I would ensure that you are running the Powershell as Administrator. If that doesn’t work, try piping directly from Get-CimInstance:

$ProfilesToDelete = Get-CimInstance -Class Win32_UserProfile -filter "special = false" | foreach {
    $User.Delete()
}

Did you try to search for it first? Most of the time you are not the first with a particular task and there are probably already solutions out there. So no need to invent the wheel again and again … :wink:

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