How to Delete a SharePoint User Profile using PowerShell?
Generally, SharePoint user profiles will get deleted when any of the below incidents happen:
- 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. This blog post will show you how to use PowerShell to delete a user profile in SharePoint.
PowerShell to delete a User Profile in SharePoint:
Here is the PowerShell to delete a user profile in SharePoint:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Configuration Variables
$SiteURL = "https://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 the user profile store. Although many profiles exist in the 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 = "https://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 them? Sure:
$DomainPrefix = "Crescent"
$UserProfiles = $UserProfileManager.GetEnumerator()| Where-Object {$_.Accountname -like "$($DomainPrefix)\*"}
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.
You have to set Filters in User Profile Sync: Filters in SharePoint User Profile Synchronization Connections
Please can you tell me where the domain qualifier goes within the section? thanks
Here you go:
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
great script! I want to add a minor correction in the removal part should be $UserProfileManager.RemoveUserProfile instead of $ProfileManager.RemoveUserProfile
Fixed! Thanks.
How about for SharePoint Online? 🙂 Cannot find the right way to do this.
Well, user profile delete is not implemented in CSOM so far!