Deleting User Profiles with PowerShell

<# Deletes user profiles for all local users. @author Brian Reich <> @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(); }
1 reply
  1. Robert Logan says:

    I’m having trouble with the syntax in line 14. I’d like to skip the following user: missoula\rl123. Can you provide an example of how I would skip this user?

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

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