Deleting User Profiles with PowerShell
<#
Deletes user profiles for all local users.
@author Brian Reich <breich@reich-consulting.net>
@copyright Copyright (C) 2013 Reich Web Consulting
@version 1.0
#>
# Get a list of all user profiles
$users = Get-WmiObject Win32_UserProfile;
# If you have any profiles you want to explicitly ignore, add them here.
$skip = @( );
Write-Host "Cleaning up user profiles..." -ForegroundColor Magenta
foreach( $user in $users ) {
# Normalize profile name.
$userPath = (Split-Path $user.LocalPath -Leaf).ToLower()
# If the profile name was found in the skip list, don't process it.
if( $skip -contains $userPath ) {
Write-Host "Skipping $userPath from ignore list.";
continue;
}
# You can't delete the profile of the currently logged-in user, so skip it.
if( $userPath -eq $env:username ) {
Write-Host "Skipping $userPath because it belongs to the current user.";
continue;
}
# If the profile belongs to a "special" account (network/system), skip it.
if( $user.Special -eq $true ) {
Write-Host "Skipping $userPath because it is a special system account.";
continue;
}
# If we got this far it's safe to delete.
Write-Host "Deleting profile for $userPath...";
$user.Delete();
}