Print Shortlink

Bulk update SharePoint 2010 user profile properties

Whether you’re SharePoint developer or IT Pro, you probably heard all about user profile service application.
One of the great features of user profile service is the ability to save a contextual user information which is going to be available to your custom components for the given user name as they navigate through your site.
You might want to access profile information for a given user and say update a web part on the home page to reflect the content for that user.
When you introduce new functionality like that to your existing web parts, you probably want to provision initial values to user profiles first.
In this post, I want to share exactly how you can use PowerShell to search for user profiles with a certain parameter provided in $ProfileSearchTerm variable and assign a property of Floor a value of “2” for each of the found profile.

Open SharePoint Management Shell from your sandbox environment and execute the following script (replace parameter config section with your own values):

$SiteUrl = "http://www.contoso.com"
$ProfileSearchTerm = "Marketing"
$PropertyValue = "2"
$PropertyName = "Floor" 

Write-Host "Loading user profile assemblies"
[System.Reflection.Assembly]::LoadWithPartialName
("Microsoft.Server")
[System.Reflection.Assembly]::LoadWithPartialName
("Microsoft.Server.UserProfiles") = Get-SPSite | Where-Object {$_.Url -eq $SiteUrl}
  if($SPSite -ne $null) 
  {
  	Write-Host "Loading current server context"
  	$ServerContext = [Microsoft.Server.ServerContext]::
GetContext($SPSite)

	$ProfileManager = New-Object Microsoft.Server.
UserProfiles.UserProfileManager($ServerContext)
	$Profiles = $ProfileManager.Search($ProfileSearchTerm) Write-Host "About to update: " $Profiles  Select DisplayName Write-Host "Saving profile changes" $Profiles | ForEach-Object {$_[$PropertyName].
Add($PropertyValue); $_.Commit()}
  }

Keep in mind that Floor must be provisioned before you write to it, so if you want to use your existing property – enter its name in the part of the script that stores variables on the top.

Enjoy!