Delete SharePoint User Profile using PowerShell

Generally SharePoint user profiles will get deleted when any of the below incident happens:
  • User account is deleted in Active Directory
  • Account is disabled
  • User Profile Service Application Connection is changed or recreated
  • Account is recreated in AD or moved out of an OU which is not part of SharePoint User Profile sync connection
At times, You may have to delete SharePoint user profiles manually.
how to delete user profiles in sharepoint using powershell

PowerShell to Delete a User Profile in SharePoint:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration Variables
$SiteURL = "http://mypage.crescent.com"
$AccountName="Crescent\Sherif"

#Get Objects
$ServiceContext  = Get-SPServiceContext -site $SiteURL
$UserProfileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext)

#Get the User Profile
$UserProfile = $UserProfileManager.GetUserProfile($AccountName) 

#remove user profile
$UserProfileManager.RemoveUserProfile($AccountName);  

Delete All User Profiles in SharePoint using PowerShell
In a scenario, I got a requirement of cleaning up user profile store. Although many profiles exists in user profile store, they were not shown in SharePoint 2013 Central administration. So, Deleting orphan/corrupted user profiles from  Central administration is not possible.  To remove all user profiles, use this PowerShell script:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration Variables
$SiteURL = "http://mypage.crescent.com"

#Get Objects
$ServiceContext  = Get-SPServiceContext -site $SiteURL
$UserProfileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext)

#Ger all User Profiles
$UserProfiles = $UserProfileManager.GetEnumerator()
 
# Loop through user profile
Foreach ($Profile in $UserProfiles) 
{
    write-host Removing User Profile: $Profile["AccountName"] 
    
    #Remove User Profile
    $UserProfileManager.RemoveUserProfile($profile["AccountName"])
}
Can I Get All User Profiles from a specific domain and delete? Sure:
$DomainPrefix = "Crescent"
$UserProfiles = $UserProfileManager.GetEnumerator()| Where-Object {$_.Accountname -like "$($DomainPrefix)\*"}
Delete SharePoint User Profile using PowerShell Delete SharePoint User Profile using PowerShell Reviewed by Salaudeen Rajack on July 03, 2015 Rating: 5

7 comments:

  1. How about for Sharepoint Online? :) Cannot find the right way to do this.

    ReplyDelete
    Replies
    1. Well, user profile delete is not implemented in CSOM so far!

      Delete
  2. great script! I want to add a minor correction in the removal part should be $UserProfileManager.RemoveUserProfile instead of $ProfileManager.RemoveUserProfile

    ReplyDelete
  3. I am new to powershell - I managed to use your code to delete individual user accounts thank you (they deleted even though I got an error message) BUT when you add the qualifying lines about just finding those from a specific domain can you please tell me where in the entire set of code they actually need to go?
    Thanks

    ReplyDelete
  4. Please can you tell me where the domain qualifier goes within the section? thanks

    ReplyDelete
    Replies
    1. Here you go:
      Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

      #Configuration Variables
      $SiteURL = "http://your-sharepoint-site-url"
      $DomainPrefix = "Crescent"

      #Get Objects
      $ServiceContext = Get-SPServiceContext -site $SiteURL
      $UserProfileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext)

      #Ger all User Profiles of a domain
      $UserProfiles = $UserProfileManager.GetEnumerator()

      # Loop through user profile
      Foreach ($Profile in $UserProfiles)
      {
      write-host Removing User Profile: $Profile["AccountName"]

      #Remove User Profile
      $UserProfileManager.RemoveUserProfile($profile["AccountName"])
      }

      Delete

Please Login and comment to get your questions answered!

Powered by Blogger.