SharePoint Online: Update User Profile Properties using PowerShell
Requirement: Update User Profile Property in SharePoint Online using PowerShell.
How to Update User Profiles in SharePoint Online?
As an administrator of a SharePoint Online environment, you may need to update user profile properties from time to time. In this blog post, we will walk through the steps necessary to update user profile properties using SharePoint Admin Center and PowerShell.
To update SharePoint Online user profile properties, follow these steps:
- Login to SharePoint Online Admin Center >> Click on the “User Profiles” link from the left navigation.
- Click “Manage User Profiles” under the People tab in User Profiles. Use Search to find the user profile to update >> Click on the “Edit My Profile” link from the context menu drop-down of the user result.
- Update any allowed user profile property and click on the “Save and Close” button.
While the above steps are fairly simple and straightforward, they may not be the best if you are looking to update the properties of multiple user profiles. Luckily, PowerShell can help you change these properties with ease.
SharePoint Online: Update user profile using PowerShell
Let’s update the “Department” field of the SharePoint user profile. Here is the PowerShell for SharePoint Online to set User profile properties.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll"
Function Update-UserProfileProperty()
{
param
(
[Parameter(Mandatory=$true)] [string] $AdminCenterURL,
[Parameter(Mandatory=$true)] [string] $UserAccount,
[Parameter(Mandatory=$true)] [string] $PropertyName,
[Parameter(Mandatory=$true)] [string] $PropertyValue
)
Try {
#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminCenterURL)
$Ctx.Credentials = $Credentials
#Get the User
$User = $Ctx.web.EnsureUser($UserAccount)
$Ctx.Load($User)
$Ctx.ExecuteQuery()
#Get User Profile
$PeopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($Ctx)
#update User Profile Property
$PeopleManager.SetSingleValueProfileProperty($User.LoginName, $PropertyName, $PropertyValue)
$Ctx.ExecuteQuery()
Write-host "User Profile Property has been Updated!" -f Green
}
Catch {
write-host -f Red "Error Updating User Profile Property!" $_.Exception.Message
}
}
#Define Parameter values
$AdminCenterURL="https://crescent-admin.sharepoint.com"
$UserAccount="i:0#.f|membership|[email protected]"
$PropertyName="Department"
$PropertyValue="Operations - IT"
#Call the function
Update-UserProfileProperty -AdminCenterURL $AdminCenterURL -UserAccount $UserAccount -PropertyName $PropertyName -PropertyValue $PropertyValue
The above script shows how to update the user profile property “department” in SharePoint Online using PowerShell.
Set User Profile Property for All Users using PowerShell
Let’s update the properties of all user profiles this time:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll"
#Config parameters
$AdminSiteUrl = "https://crescent-admin.sharepoint.com"
$PropertyName = "Fax"
$PropertyValue = "+971-043322001"
#Get Credentials
$Cred = Get-Credential
Try {
#Connect to AzureAD
Connect-AzureAD -Credential $Cred
#Get All Users from AzureAD Domain - Exclude Guests
$AllUsers = Get-AzureADUser -All:$True -Filter "UserType eq 'Member'"
Write-host "Total Number of User Profiles Found:"$AllUsers.Count
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminSiteUrl)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Get User Profile Manager
$PeopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($Ctx)
$Counter = 1
ForEach($User in $AllUsers)
{
#update User Profile Property
$UserAccount = "i:0#.f|membership|$($User.UserPrincipalName)"
$PeopleManager.SetSingleValueProfileProperty($UserAccount, $PropertyName, $PropertyValue)
$Ctx.ExecuteQuery()
Write-host "User Profile Property has been Updated for: $($User.UserPrincipalName)" -f Green
$Counter++
Write-Progress -Activity "Updating User Profile Data..." -Status "Updating User Profile $Counter of $($AllUsers.Count)" -PercentComplete (($Counter / $AllUsers.Count) * 100)
}
}
Catch {
write-host -f Red "Error Updating User Profile Property!" $_.Exception.Message
}
Update SharePoint Online user profile properties using PowerShell
There are fields with Multi-values in the People profile. E.g. Skills! Let’s update Multi-valued fields in SharePoint Online user profiles.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll"
Function Update-MultiUserProfileProperty()
{
param
(
[Parameter(Mandatory=$true)] [string] $AdminCenterURL,
[Parameter(Mandatory=$true)] [string] $UserAccount,
[Parameter(Mandatory=$true)] [string] $PropertyName,
[Parameter(Mandatory=$true)] [System.Collections.Generic.List``1[System.string]] $PropertyValues
)
Try {
#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminCenterURL)
$Ctx.Credentials = $Credentials
#Get the User
$User = $Ctx.web.EnsureUser($UserAccount)
$Ctx.Load($User)
$Ctx.ExecuteQuery()
#Get User Profile
$PeopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($Ctx)
$PropertyValues
#update User Profile Property
$PeopleManager.SetMultiValuedProfileProperty($User.LoginName, $PropertyName, $PropertyValues)
$Ctx.ExecuteQuery()
Write-host "User Profile Property has been Updated!" -f Green
}
Catch {
write-host -f Red "Error Updating User Profile Property!" $_.Exception.Message
}
}
#Define Parameter values
$AdminCenterURL="https://crescent-admin.sharepoint.com"
$UserAccount="i:0#.f|membership|[email protected]"
$PropertyName="SPS-Skills"
$PropertyValues = New-Object "System.Collections.Generic.List``1[System.string]"
$PropertyValues.Add("SharePoint")
$PropertyValues.Add("PowerShell")
$PropertyValues.Add("C#")
#Call the function
Update-MultiUserProfileProperty -AdminCenterURL $AdminCenterURL -UserAccount $UserAccount -PropertyName $PropertyName -PropertyValues $PropertyValues
In another case, when I had to update the language preferences of the User Profile in SharePoint Online, I specified “SPS-MUILanguages” as Property Name and “en-US,en-GB,de-DE” as choices.
SharePoint Online: PnP PowerShell to Set User Profile Property
Here is how to update SharePoint Online user profile properties using the PnP PowerShell cmdlet Set-PnPUserProfileProperty:
#Config Variables
$AdminSiteURL = "https://Crescent-admin.sharepoint.com"
$UserAccount= "[email protected]"
#Connect to PnP Online
Connect-PnPOnline -Url $AdminSiteURL -Interactive
#sharepoint online powershell update user profile property
Set-PnPUserProfileProperty -Account $UserAccount -PropertyName "Department" -Value "Operations - IT"
Similarly, you can update multivalued user profile properties using PowerShell
Set-PnPUserProfileProperty -Account $UserAccount -PropertyName "SPS-Skills" -Values "SharePoint", "PowerShell"
If you want to sync a user profile property from Azure AD, use SharePoint Online: Sync User Profile Property from Azure AD using PowerShell
Great thanks! Exactly what I’m looking for. How about updating multiple users?
Article has been updated with the script to update multiple users!