Batch Update User Profile Properties

Learn Expert

A few months back, the Patterns and Practices team at Microsoft announced a new User Profile Batch Update API.  It's great for updating large batches of properties, but only works for user profile properties, which have not been set to be editable for the end users to avoid situation where the user profile import process would override any information which end user has already updated.  This makes a lot of sense.  But what if you need to do some changes to these editable fields for a number of users?


To do just that, I wrote a script using some of the OfficeDev PnP PowerShell library.  In order to use it, you need to consider the following:

  • The input is a CSV file (sample attached to the post)
  • Log onto the admin site (e.g.
  • The first row of the CSV must be the InternalName properties of the fields
  • The first column should be the Email of the user you're modifying
  • If you want to add multiple values to a field, they should be separated by a pipe (|)
  • For People Picker fields, use the AccountName (e.g. i:0#.f|membership|
  • Since querying user profile property fields returns all the values as text, the code doesn't check for field types and tries to save the values provided as-is.  If it fails, it will report an error to the console.


Function PopulateUserProfiles
            [string] $csvFile

	 $ctx = Get-SPOContext
        Write-Host "Please connect to tenant admin site before running this function."

     if (Test-Path $csvFile)
        $UserData = Import-Csv $csvFile
	    $rows = $UserData | measure
	    $ColumnName = $UserData | get-member | ? {-not($_.Name -in @("Equals", "GetHashCode", "GetType", "ToString"))} | select "Name"

	    for ($i = 0; $i -lt $rows.Count; $i++)
            $Email = $UserData[$i].($ColumnName[0].Name)
            Write-Host "Updating data for $Email"

            for ($j = 1; $j -lt $ColumnName.Count; $j++)
                # if there are multiple values, then I separate them using the |.  However, if a person field is used, then the format is using the claims encoding which also includes
                # the |.  So first I check if it is a user account or not and act accordingly.  See
                # for more information about the claims format.
                $value =  $UserData[$i].($ColumnName[$j].Name)

                # if the first characters are i:0 or c:0, then it's a user account and I leave it as-is.
                if (($value.Substring(0,3) -eq "i:0") -or ($value.SubString(0,3) -eq "c:0"))
                    Set-SPOUserProfileProperty -account $Email -PropertyName $ColumnName[$j].Name -Values $value -ErrorAction SilentlyContinue 
                    # split the string using the | as a delimiter and load the values into the field.
                    Set-SPOUserProfileProperty -account $Email -PropertyName $ColumnName[$j].Name -Values $value.Split("|") -ErrorAction SilentlyContinue 

                if ($?)
                   Write-Host "  Set $($ColumnName[$j].Name) --> $($UserData[$i].($ColumnName[$j].Name))." -ForegroundColor Green
                   Write-Host "  Could not set $($ColumnName[$j].Name) --> $($UserData[$i].($ColumnName[$j].Name)).  $($error[0].Exception.message)" -ForegroundColor Red

I have tested out the code on a number scenarios that worked.  If you find any issues, please let me know and I'll try to correct them.


I hope you find it helpful!

3 Replies

Thanks for Sharing!

@Haniel Croitoru this reply is a couple of years late but your script is still relevant - thanks for sharing! After some slight adjustments to fit the new PnP cmdlets it works like a charm. However, I am running to an issue and I was hoping someone might have the answer.


One of the properties I am trying to update is the SPS-TimeZone, but it fails to accept the integer representing the SPS-TimeZone ID.


Setting the Time Zone separately works well, like this for example:

Set-PnPUserProfileProperty -Account $upn -Property 'SPS-TimeZone' -Value 10


But when I do this using the script, I get the following error:

Could not set SPS-TimeZone --> 3.  Exception calling "Substring" with "2" argument(s): "Index and length must refer to a location within the string.
Parameter name: length"


And if I add it as a string, I get this:

Could not set SPS-TimeZone --> "45".  Invalid time zone format. The value must be SPTimeZone object, a valid integer time zone ID, or a string representation of a valid integer time zone ID.




 Yes, thanks for sharing, this post helped me a lot!
In the same spirit, I just want to add, that I experience no problem with setting SPS-TimeZone as long as I first set

Set-PnPUserProfileProperty -Account $upn -PropertyName 'SPS-RegionalSettings-FollowWeb' -Value $false
Set-PnPUserProfileProperty -Account $upn -PropertyName 'SPS-TimeZone' -Value '3'


List of TimeZone IDs are at