I needed to quickly update a User Profile photo using PowerShell and was running the following code snippet for PowerShell as a farm administrator:
$mySiteUrl = "http://mysite.mydomain.com";
$mySiteProperty = "PictureUrl";
$userAccount = "contoso\rcerda";
$userPhoto = "http://intranet.mydomain.com/sitecollectionimages/rcerda.jpg";
$mySiteHostSite = Get-SPSite $mySiteUrl
$mySiteHostWeb = $mySiteHostSite.OpenWeb()
$serviceContext = Get-SPServiceContext $mySiteHostSite
$profileManager = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($serviceContext)
$userProfile = $profileManager.GetUserProfile($userAccount)
$userProfile.get_Item($mySiteProperty).Value = $userPhoto;
$userProfile.Commit()
But I kept getting the following cryptic PowerShell error on the highlighted line above: Exception calling ".ctor" with "1" argument(s): "Object reference not set to an instance of an object."
What?!
The first thing I did was double check my parameters and tried it again about 15 times. Then I got smart and started looking around to see if anyone else encountered this. Turns out they had. The common denominator seemed to be a permissions issue, but I was logged in as a farm administrator.
Well, it turns out that is not enough. To resolve this issue, you need to log into Central Administration and follow these steps:
- Choose Application Management à Manage service applications.
- Click on the User Profile Service Application to highlight it and choose Permissions.
- Explicitly add your farm administrator account to the User Profile Service Application with Full Control.
- And whalah! Problem solved.