SharePoint User Profile delete script:

I happened to work on a project where I’m supposed to delete all the user profiles in a SharePoint farm as we were having some problem with the FIM sync to SharePoint. Since we didn’t find much time to troubleshoot the issue we planned to delete all the user profiles from the farm and then do a complete FIM full sync.

Now, I had two options in my mind to do this,

  1. Finding a script which can do this for us.
  2. Deleting the User profile Service application completely and rebuilding it from the scratch.

Obviously the latter one is not a good choice and hence we decided to go with the script.

Things to consider before running the script:

  1. Make sure you’re running this script by logging into the SharePoint server using the farm admin account.
  2. Make sure the account running this script has enough permissions on the User profile service application ( Central Admin–>Manage service applications–>User profile service application –>Check whether the account running this script has been added to the administrators group as well to the “permissions” section in the top ribbon interface and has been given full control as well )
  3. Please remember that this script will only be deleting the user profiles tied to the user accounts in the SharePoint farm and not the account themselves.

#Input parameters:

  1. Mention the “My site “host site collection url
  2. Mention the do not delete setup admin account name ( your farm admin account )

Please find the command below:

#Add SharePoint PowerShell SnapIn if not already added

if ((Get-PSSnapin “Microsoft.SharePoint.PowerShell” -ErrorAction SilentlyContinue) -eq $null) {

Add-PSSnapin “Microsoft.SharePoint.PowerShell”

}

$site = new-object Microsoft.SharePoint.SPSite(“http://skvkfm-it01/”);

$ServiceContext =[Microsoft.SharePoint.SPServiceContext]::GetContext($site);

#Get UserProfileManager from the My Site Host Site context

$ProfileManager = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext)

$AllProfiles = $ProfileManager.GetEnumerator()

 

foreach($profile in $AllProfiles)

{

$DisplayName = $profile.DisplayName

$AccountName = $profile[[Microsoft.Office.Server.UserProfiles.PropertyConstants]::AccountName].Value

#Do not delete setup (admin) account from user profiles. Please enter the account name below

if($AccountName -ne “Domain\MySiteSVApp”)

{

$ProfileManager.RemoveUserProfile($AccountName);

write-host “Profile for account “, $AccountName, ” has been deleted”

}

 

}

write-host “Finished.”

$site.Dispose()

Ref : _ You can find the script here :

https://gallery.technet.microsoft.com/office/Delete-all-User-Profiles-fa4e1428

 

 

 

 

 

 

 

 

Leave a comment