Delete User Profile

The other day I was in need of deleting over 100 profiles and found the built-in Windows tool to be inefficient for this task.  I came across some PowerShell code that was a life saver.  Using the code below I was able to write a script and automate the task.

Finding the User Profile:

$username = "user"

$computername = “computer”

$userProfile = Get-WmiObject -computer $computername win32_userprofile -filter “LocalPath like `’%\\$username`'”

Note: Edit $computername variable with the name of the computer with the profile on it.  Yes this can be done remotely.  Also edit the $username variable with the user you are looking for.

Verify Correct User Profile:

$userProfile

Note: You do not want to delete the wrong user profile.  Always double check.

Deleting the User Profile:

$userProfile | Remove-WmiObject

Note: This code is only for users that have been properly logged off and their registry hive has been unloaded.  If you see an error, one of those two reasons may be the problem.   Restarting before you delete is one way to make sure the registry hive is unloaded.


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: