Friday, July 3, 2015

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.

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

#Configuration Variables
$SiteURL = "http://mypage.crescent.com"
$UserLogin="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($UserLogin) 
#remove user profile
#Get User Profile $ProfileManager.RemoveUserProfile($AccountName);  

Delete all user profiles in SharePoint
In an another 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
    $ProfileManager.RemoveUserProfile($profile["AccountName"])
}



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...