SharePoint: Get Set and Copy User Profile Properties using PowerShell

SharePoint: Get Set and Copy User Profile Properties using PowerShell



Introduction


Whether you're a SharePoint Administrator or SharePoint Developer, being able to quickly read, update or copy User Profile fields is a handy skill to have. Using PowerShell to get and set User Profile fields is both quick and easy. This post outlines how to do it!

Applies To


  • SharePoint 2010
  • SharePoint 2013

Getting the User Profile


The basic PowerShell code for getting a user profile, using a users UPN (User Principal Name):
[void][reflection.assembly]::Loadwithpartialname("Microsoft.Office.Server");            
$site=new-object Microsoft.SharePoint.SPSite("https://c05470sp10:7443");            
$serviceContext = Get-SPServiceContext $site;            
$site.Dispose();            
$upm = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($serviceContext);            
$userProfile = $upm.GetUserProfile("myarlett@company.com");



The basic PowerShell code for getting a user profile, using the user's login name:
[void][reflection.assembly]::Loadwithpartialname("Microsoft.Office.Server");            
$site=new-object Microsoft.SharePoint.SPSite("https://c05470sp10:7443");            
$serviceContext = Get-SPServiceContext $site;            
$site.Dispose();            
$upm = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($serviceContext);
$userProfile = $upm.GetUserProfile("company\myarlett");


Listing all the Profile Properties (Fields) and their Types


List the user profile properties (including the field type). This is handy, because we'll need to know what the field type is before trying to set it's value:
$userProfile.Properties | sort DisplayName | FT DisplayName,Name,@{Label="Type";Expression={$_.CoreProperty.Type}}



Getting the Value of a Property

Get the users About Me property (HTML):
$userProfile["AboutMe"].Value

Setting the Values of Properties


Update the users Location (String field):
$userProfile["SPS-Location"].Value = "London";            
$userProfile.Commit();

Update the users Manager (Person field):
$userProfile["Manager"].Value = (Get-SPWeb https://c05470sp10:7443).EnsureUser("company\fred");
$userProfile.Commit();
Note that in the above example, we have retrieved an SPUser object (for the manager) from the Central Admin site, using the EnsureUser method.

Add a value to the About Me property (multi-string)
$rp = $userProfile["SPS-Responsibility"]            
#Print out the current values            
foreach($s in $rp){$s}             
#Add a new value to the UserProfileValueCollection            
$rp.Add("Awesomeness");            
#Set the SPS-Responsibility property with the UserProfileValueCollection            
$userProfile["SPS-Responsibility"].Value = $rp;            
#Save the profile changes back to the User Profile store            
$userProfile.Commit()

Clear all values in the About Me property
#Delete all values in the UserProfileValueCollection            
$rp.Clear()            
#Set the SPS-Responsibility property with the UserProfileValueCollection            
$userProfile["SPS-Responsibility"].Value = $rp;            
#Save the profile changes back to the User Profile store            
$userProfile.Commit()


Copying User Profile Properties between Profiles


Copy fields from one user profile to another:
$userProfile2 = $upm.GetUserProfile("company\matthewette");            
$userProfile2["AboutMe"].Value = $userProfile["AboutMe"];            
$userProfile2.Commit();

See Also


References



Sort by: Published Date | Most Recent | Most Useful
Comments
  • I need to do this in SharePoint 2013 on a client's site. they do not want me use AD UPS sync so the option you describe looks the way to go.   I take it the profile update will need to be done once per farm ( or Web App) or in in other words the MYsites Web App  that is being hosted on that farm?  In your post you have a url which looks like the main web App home site.    

  • Hey Dan,

    Have they already created all the profiles on the SharePoint 2013 farm? Do you need to migrate profiles, or is this to setup new profiles?

    The techniques described above can be used to add or update user profile properties in any scenario, though it does assume that the profile has already been created.

    If the farm is a basic farm with one User Profile Service, then you're best to get the service context from the Central Administration site (the first step above, Getting the User Profile). The URL I used in my example was for the Central Administration site.

    Hope that helps!

  • Can you show an example using powershell to set the User Profile property "SPS-Responsibility", AKA "Ask Me About" property? As this property is of a type string (multi value) we have had no success in setting values in this property.

  • Hi sspeake,

    I've added an example for setting / clearing the About Me property. The type of object is Microsoft.Office.Server.UserProfiles.UserProfileValueCollection.

    The methods available on this property are documented here: msdn.microsoft.com/.../microsoft.office.server.userprofiles.userprofilevaluecollection_methods.aspx

Page 1 of 1 (4 items)