Search result for:
Press ESC to close Search Result or press here

n8design


Art, Design, Media, Web & SharePoint … by Stefan Bauer
11. November 2010

Clean up SharePoint User Profile Store using PowerShell

Today i had a problem in my development environment for my SharePoint Projects regarding User Profile Store. My user profile was a messed up and was not available for editing in central administration. When i searched for my profile it still there but not shown for administration.  This was a really strange behavior because a couple of days it worked well. I added various new profile properties to my user profile service application. So the recreation of User Profile Service Application was not an option. Deletion of orphan or corrupted user profiles is not possible using Central Administration or even using avaliable PowerShell commands.

But there is a solution using PowerShell without compiled code. Using PowerShell everything what is avaliable in the server object model is avaliable. First of all two assemblies must be referenced.

These assemblies are:

Those assemblies could be referenced using System.Reflection and the rest of the script is quite simple SharePoint Development. So get a context object, open profile service application get all use and delete them.

/* Load required Assemblies for SharePoint Server and User Profile */
[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.Office.Server”)
[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.Office.Server.UserProfiles”)

/* Central Adminstration URL or any Web Application*/
$url = "http://myserver:Port"
/* Create a new Context Object */
$contextWeb = New-Object Microsoft.SharePoint.SPSite("http://servername");

/* Get the right Service Context */
$ServerContext = [Microsoft.Office.Server.ServerContext]::GetContext($contextWeb);

/* create a new connection to the UserProfileManager */
$UserProfileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServerContext);

/* Ger all User Profiles */
$Profiles = $UserProfileManager.GetEnumerator();

/* Loop through user profile */
foreach ($oUser in $Profiles ) {

	/* Remove Profile */
	$UserProfileManager.RemoveUserProfile($oUser.item("AccountName"));
}

This script can be extended to delete only specific users from user profile information. At the end I was able to solve my problem. My User Profile was deleted and recreated on first access and due profile import. Everything worked fine.
In the old days you had to write some custom command line tool to address those batch update and deletion task. With the introduction of PowerShell to SharePoint any possible administrative task could be accomplished by just use scripting.

Comments
  • Larry W Virden commented at 25. January 2013 at 16:49

    I would like to compare the account name in the site collection user profile table with the account name in the farm level user profile and a) get a list of all the site collection user profiles that differ from the farm and b) have an option to update the site collection information.

    Are you aware of any articles that discuss doing something like this in powershell?
    Thank you

    • Stefan Bauer commented at 28. January 2013 at 14:15

      Tricky questions. When you iterate over the profiles you can export that information to the file system and import it to excel. For the Site Collection thing you can create another PowerShell script that gets all items of the so called “User Information List”.
      You can compare the user information using Excel.

      For updating the information you should check out the following blog post: Updating SharePoint User Information


%d bloggers like this: