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?
To update SharePoint Online user profile properties, follow these steps:
  • Login to SharePoint Online Admin Center >> Click on "User Profiles" link from the left navigation
  • In User Profiles, Click on "Manage User Profiles" under People tab. Use Search to find the user profile of the user to update >> Click on "Edit My Profile" link from the context menu drop down of the user result.
  • Update any allowed user profile property and click on "Save and Close" button.
    powershell sharepoint online user profile properties

SharePoint Online: Update user profile using PowerShell
Lets update the "Department" field of 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

Set User Profile Property for All Users using PowerShell
#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
    $AllUsers = Get-AzureADUser -All:$True
    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 People profiles. 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 language preferences of 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 PnP PowerShell
#Config Variables
$AdminSiteURL = "https://crescenttech-admin.sharepoint.com"
$UserAccount= "[email protected]"

#Connect to PnP Online
Connect-PnPOnline -Url $AdminSiteURL -Credentials (Get-Credential)

#sharepoint online powershell update user profile property
Set-PnPUserProfileProperty -Account $UserAccount -PropertyName "Department" -Value "Operations - IT"
Similarly, you can update multi-valued user profile 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
SharePoint Online: Update User Profile Properties using PowerShell SharePoint Online: Update User Profile Properties using PowerShell Reviewed by Salaudeen Rajack on January 25, 2017 Rating: 5

2 comments:

  1. Great thanks! Exactly what I'm looking for. How about updating multiple users?

    ReplyDelete
    Replies
    1. Article has been updated with the script to update multiple users!

      Delete

Please Login and comment to get your questions answered!

Powered by Blogger.