Categories

Working with profiles: 2 deleting profiles

I recently (1 June) showed how to discover the user profiles on your system. Now its time to delete them.

function remove-profile {            
 param (            
  [parameter(Mandatory=$true)]            
  [string]$username            
 )            
            
 $user = Get-CimInstance -Class Win32_UserAccount -Filter "Name = '$username'"             
 $profile = Get-CimInstance -Class Win32_UserProfile -Filter "SID = '$($user.SID)'"            
 $folder = Split-Path -Path $profile.LocalPath -Leaf            
            
 if ($folder -eq $username){            
  Remove-CimInstance -InputObject $profile            
 }            
 else {            
  Write-Warning -Message "Could not resolve profile and user name"             
 }            
            
}


I’m going to start with the CIM cmdlets as these are the way of the future in PowerShell v3.



Start by taking a user name as a parameter. Get the Win32_UserAccount class object representing that account. use the SID to find the profile via Win32_UserProfile.  Take the profile’s localpath and split it. The last part of the path should match the username – if it does then delete the profile otherwise throw a warning. Deleting the profile does delete the folder under c:\users



If you have to use the WMI cmdlets then its very similar



function remove-profile {            
 param (            
  [parameter(Mandatory=$true)]            
  [string]$username            
 )            
            
 $user = Get-WmiObject -Class Win32_UserAccount -Filter "Name = '$username'"             
 $profile = Get-WmiObject -Class Win32_UserProfile -Filter "SID = '$($user.SID)'"            
 $folder = Split-Path -Path $profile.LocalPath -Leaf            
            
 if ($folder -eq $username){            
  Remove-WmiObject -InputObject $profile            
 }            
 else {            
  Write-Warning -Message "Could not resolve profile and user name"             
 }            
            
}


Just the name of the cmdlets change.



You can’t use WMI to delete local accounts as explained on page 363 of PowerShell and WMI 



If you have profiles generated by AD accounts you’ll need to find the SID from the AD account and use that as the filter for deletion

3 Responses to Working with profiles: 2 deleting profiles

  • Pete Gomersall says:

    Richard,
    There seems to be problems with this script. On my system which is W8 x64 in domain the section to get the user – $user = Get-CimInstance -Class Win32_UserAccount -Filter “Name = ‘$username’” can return multiple accounts if the same username exists on local machine and in the domain – the script then fails.
    Pete

  • Lee dandridge says:

    Do to a merger I need to clean up a large group of XP user profiles before migrating them to our AD forest. I only want to delete user id’s that have a userid.domain format. Can I list profiles with your script and then selectively delete the ones I want?

  • RichardSiddaway says:

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>