SharePoint 2013: 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");

http://3.bp.blogspot.com/-_xEZsEyt_0s/UnEpB3OQO4I/AAAAAAAAARc/6OE0AhpraWY/s1600/upu-02a.png

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");

http://2.bp.blogspot.com/-y2goDLRkxek/UnEpB5sQrPI/AAAAAAAAARQ/KIAGAGCZdbA/s1600/upu-02.png

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}}

http://2.bp.blogspot.com/-GtGnkPwGp7Q/UnEpB5ZbFtI/AAAAAAAAARM/h6wcJy6UPjs/s1600/upu-3.png

Getting the Value of a Property

Get the users About Me property (HTML):

$userProfile["AboutMe"].Value

http://3.bp.blogspot.com/-JIb_qYa6dlc/UnLLvOefghI/AAAAAAAAARo/Qw352e7Qx4s/s1600/upu-4-final.png

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

 

Full Script

https://gallery.technet.microsoft.com/Export-SharePoint-User-04fbc24d