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…

16 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
  4. Our digital marketing course in Chennai is targeted at those who are desirous of taking advantage of career opportunities in digital marketing. Join the very best Digital Marketing Course in Chennai. Get trained by an expert who will enrich you with the latest digital trends.

    Digital Marketing Course in Chennai
    Digital Marketing Training in Chennai
    Online Digital Marketing Training
    SEO Training in Chennai
    Digital Marketing Course
    Digital Marketing Training
    Digital Marketing Courses

    ReplyDelete
  5. This article is interesting and useful. Thank you for sharing. And let me share an article about health that God willing will be very useful. Thank you :)

    Cara Menghilangkan Bruntusan
    Obat Penyakit Susah Kencing
    Khasiat dan manfaat QnC Jelly Gamat

    ReplyDelete
  6. No matter what type of Michael Kors Bags Outlet each individual got, they all had one thing in common. We all know that getting something on sale is like winning a mini lottery, but to find a name brand handbag like Michael Kors Black Friday Sale for instance, discounted is like a slice of heaven on earth. I have personally attacked my husband with hugs and kisses when finding Official Coach Outlet Online at discounted prices.

    Michael Kors Factory Outlet stores can be found in malls all over. MK Outlet and other Coach merchandise can also be found in some boutiques. Inspired by the same material baseball gloves are made from, these Coach Bags On Sale Online are versatile and stylish. Since it's conception, the Michael Kors Handbags Outlet has been produced into a variety of designs, colors, shapes, and styles that have won top pick of millions of women the world over.

    New Michael Kors Bags are the most sought after handbag. Since Coach Outlet Online is one of the most recognized name brands in the world of fashion, you will find A-list celebrities down to small girls wearing them.

    tags:Coach Outlet|Coach Bags Factory Outlet|Coach Purses Outlet

    ReplyDelete