Deleting User Profiles Using PowerShell

There are many reasons why we would want to use a PowerShell shell script to delete user profiles.

  1. We don’t want to use or trust the My Site Cleanup job.
  2. We have a batch of stub user profiles that need to be deleted.
  3. We just want to delete some user profiles for testing purposes.
  4. We don’t want to wait 30 days before the User Profile is deleted by the My Site Cleanup timer job(applies only to SharePoint 2016)

NOTE: Deleting user profiles with this script will delete just the user profile.  This will not delete their My Site or send out that notification like the My Site cleanup job. WARNING: This script will delete user profiles specified in the file. The only other means to recover the user profile is to restore the Profile DB from backup.

The List

There are a few ways that we can gather a list of user profiles. We can query the profile database, run GetNonImportedObjects, or we already have a list that we want to delete.

  1. Profile DB query – Select NtName from UserProfile_Full with(nolock) where bdeleted=1
  2. PowerShell:


$upa = Get-spserviceapplication | ?{$_.typename -match "profile"}
Set-SPProfileServiceApplication $upa -GetNonImportedObjects $true | out-file c:\temp\NonImportedProfiles.txt

We just want the list to be a text file with the a NTName on each line.

Parameters


We will just need to modify the top 3 lines to adjust it for our farm. The first is $siteurl. This should be URL that is consuming from the User Profile Service Application. The $file is the location and name of the input file we created above. The $log is for the location of the log file(Yes, there is logging with my PowerShell scripts). Just make sure the directory is created.

One gotcha is to make sure the account running this script has permissions to the User Profile service application. This can be accomplished in Central Admin with adding the account into Permissions with Full Control.

Link to the Script

2 thoughts on “Deleting User Profiles Using PowerShell

  1. Leandro Demier

    Thanks for the article! Although the script is no longer available, I was able to make it work combining the parts in the article and a little bit more of code, this did the trick:

    Add-PSSnapin microsoft.sharepoint.powershell

    #exporting tue users list
    $upa = Get-spserviceapplication | ?{$_.typename -match “profile”}
    Set-SPProfileServiceApplication $upa -GetNonImportedObjects $true | out-file c:\temp\NonImportedProfiles.txt

    edit the file removing the first line with this information “These objects will be marked for deletion.” and also the users that I want to stay.

    proceed with the shell
    #using the file to remove the profiles
    $site = “https://hmgportalintraligados.icatuseguros.com.br”
    $context = Get-SPServiceContext -Site $site
    $web = Get-SPWeb -Identity $site.ToString()
    $upm = New-Object -TypeName Microsoft.Office.Server.UserProfiles.UserProfileManager -ArgumentList $context
    $oldUsers = get-content c:\temp\NonImportedProfiles.txt
    $oldUsers | foreach {
    $upm.RemoveUserProfile($_)
    }

Leave a Reply

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