Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

9 thoughts on “How to Delete a SharePoint User Profile using PowerShell?

  • How to remove AD disabled/deactivated users from user profile service application. I got to delete these. I am looking for power shell script which can delete disabled users from user profile application.

    Reply
  • Please can you tell me where the domain qualifier goes within the section? thanks

    Reply
    • Here you go:

      Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
       
      #Configuration Variables
      $SiteURL = "https://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"])
      }
      
      Reply
  • 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

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

    Reply
  • How about for SharePoint Online? 🙂 Cannot find the right way to do this.

    Reply

Leave a Reply

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