Friday, 16 July 2010

Modify single value user profile properties in SharePoint with PowerShell

The two most widely used user profile property types in SharePoint 2010 are “string (Single Value)” and “string (Multi Value)”. Single value property types work in a similar way to “Single line of text” columns in lists and libraries – examples being Name, Department, Job Type, etc. Multi value property types use the Managed Metadata service to allow the multiple input of new or previously used values from the Keywords term set – examples being Skills, Schools, Responsibilities, etc.

The PowerShell script below provides an example of how to add or modify a single value user profile property for an individual user. Admittedly, this can be done quite easily by using the Central Administration UI (although it does take a few clicks), but it’s real power is when you combine the script with a CSV or XML file to bulk change hundreds of user profile properties in a matter of seconds – you can find examples of using CSV and XML files for bulk operations here and here, although I plan to publish more articles on these techniques in future posts.

Note: The user running the script (which I assume is some sort of SharePoint Administrator) must have been granted the Manage Profiles right in the Administrators section and Full Control rights in the Permissions section of the User Profile Service Application (pictured below).

UserProfileAdmin

The script below changes the value of the Web Site attribute for a user with the account name DOMAIN\Phil.Childs. You will need to specify the My Site host site collection URL for the $mySiteUrl variable:

#Set up default variables
$mySiteUrl = "http://mysite"
$adAccount = "DOMAIN\phil.childs"
$upAttribute = "WebSite"
$upAttributeValue = “http://get-spscripts.com”

#Get site objects and connect to User Profile Manager service
$site = Get-SPSite $mySiteUrl
$context = Get-SPServiceContext $site
$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context)

#Check to see if user profile exists
if ($profileManager.UserExists($adAccount))
{
    #Get user profile and change the value
    $up = $profileManager.GetUserProfile($adAccount)
    $up[$upAttribute].Value = $upAttributeValue
    $up.Commit()
}
else
{
    write-host "Profile for user"$adAccount "cannot be found"
}

#Dispose of site object
$site.Dispose()

In a future post I will run through how to use a similar approach to change a multi value user profile property…

8 comments:

  1. Thank you. I combined two of your script examples into one. This one reads from a CSV file of two columns (NTName and PropertyVal) to load the PictureURL of each profile. I needed to use this to merge two profile databases into one.

    #Set up default variables
    $csvfile="PictureURL.csv"
    $mySiteUrl = "http://sitename"
    $upAttribute = "PictureURL"

    #Get site objects and connect to User Profile Manager service
    $site = Get-SPSite $mySiteUrl
    $context = Get-SPServiceContext $site
    $profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context)

    #Create Lists from each item in CSV file
    $csvData = Import-Csv $csvfile
    foreach ($line in $csvData)
    {
    #Check to see if user profile exists
    if ($profileManager.UserExists($line.NTName))
    {
    #Get user profile and change the value
    $up = $profileManager.GetUserProfile($line.NTName)
    $up[$upAttribute].Value = $line.PropertyVal
    $up.Commit()
    }
    else
    {
    write-host "Profile for user"$line.NTName "cannot be found"
    }
    }

    #Dispose of site object
    $site.Dispose()

    ReplyDelete
  2. I tried your script on the Title Field. it worked a treat. But thought I would mention, I ran a full profile sync and it went back to the old Title?

    ReplyDelete
  3. @The Baretta - yes, it will do because the title property is managed from Active Directory by default. Any changes you make to these sorts of properties will be overwritten at the next scheduled User Profile import job

    ReplyDelete