How To Update SharePoint User Profile Properties Using PnP PowerShell

In this article, you will see the steps for updating SharePoint user profile properties for multiple users using PnP PowerShell. Here, I have stored multiple user profile details in a CSV file. So, we can use the CSV file as the input.

Prerequisite

Before you begin utilizing PowerShell to oversee SharePoint Online, ensure that the SharePoint Online Management Shell is installed. You can install the SharePoint Online Management Shell by downloading and running the SharePoint Online Management Shell. You only need to do this once for each computer from which you are running SharePoint Online PowerShell commands.

Step 1

Connect to SharePoint Admin site using Connect-PnPOnline cmdlet. The required parameters are,

URL - The SharePoint site url ((e.g. https://tenant-admin.sharepoint.com)

The following code snippet will help you to connect SharePoint sites.

  1. $siteurl="https://<tenant-admin>.sharepoint.com"  
  2. Connect-PnPOnline -Url $siteurl  

SharePoint  

Step 2

Get context instance from the SharePoint site. To update profile properties, you will need to get access to perform.

The following cmdlet helps you to get Context from the SharePoint site.

  1. $ctx = Get-PnPContext  

 

SharePoint

Step 3

Use the Import-CSV cmdlet to get the table like custom objects from the items in CSV files. Each column in the CSV file becomes the property of the custom object, and items in the rows become the property value. Import-CSV works on any CSV file, including those that are generated from the Export-CSV cmdlet.

The following cmdlets will help you to import CSV files.

  1. $csvFile=’$csvFile = 'F:\Ravishankar\Deployment\UserData.csv'  
  2. $UserData= Import-Csv $csFile  

 

SharePoint

Step 4

You can update user profile properties using Set-PnPUserProfileProperty cmdlets. The required parameters are,

  • Account
    Account of the user, either login name or email of the user

  • PropertyName
    Give the Property name, for instance SPS-Location, SPS-Department

  • Values
    Give the Property value

The following cmdlets will help you to pass data from the user data and update user profile properties,

  1. $ColumnName = $UserData | get - member | ? {-not($_.Name - in @("Equals""GetHashCode""GetType""ToString"))  
  2. } | select "Name"  
  3. for ($i = 0; $i - lt $rows.Count; $i++) {  
  4.     $Email = $UserData[$i].($ColumnName[0].Name)  
  5.     Write - Host "Updating data for $Email"  
  6.     for ($j = 1; $j - lt $ColumnName.Count; $j++) {  
  7.         $value = $UserData[$i].($ColumnName[$j].Name)  
  8.         if (($value.Length - ge 3) - and(($value.Substring(0, 3) - eq "i:0") - or($value.SubString(0, 3) - eq "c:0"))) {  
  9.             Set - PnPUserProfileProperty - Account $Email - PropertyName $ColumnName[$j].Name - Values $value - ErrorAction SilentlyContinue  
  10.         } else {#  
  11.             split the string using the | as a delimiter and load the values into the field.  
  12.             Set - PnPUserProfileProperty - Account $Email - PropertyName $ColumnName[$j].Name - Values $value.Split("|") - ErrorAction SilentlyContinue  
  13.         }  
  14.         if ($ ? ) {  
  15.             Write - Host " Set $($ColumnName[$j].Name) --> $($UserData[$i].($ColumnName[$j].Name))." - ForegroundColor Green  
  16.         } else {  
  17.             Write - Host " Could not set $($ColumnName[$j].Name) --> $($UserData[$i].($ColumnName[$j].Name)). $($error[0].Exception.message)" - ForegroundColor Red  
  18.         }  
  19.     }  
  20. }  
SharePoint

Final code

  1. $sitecollectionUrl = Read - Host 'Please Enter the Site Collection URL'  
  2. $csvFile = Read - Host 'Please Enter the Path of ur Excel (.csv)'  
  3. $credentials = Get - Credential  
  4. Connect - PnPOnline - Url $sitecollectionUrl - Credentials $credentials  
  5. $wshell = New - Object - ComObject Wscript.Shell  
  6. try {  
  7.     $ctx = Get - PnPContext  
  8. catch {  
  9.     $wshell.Popup("Please connect to tenant admin site!", 0, "Done", 0x1)  
  10. }  
  11. if ($ctx) {  
  12.     $UserData = Import - Csv $csvFile  
  13.     $rows = $UserData | measure  
  14.     $ColumnName = $UserData | get - member | ? {-not($_.Name - in @("Equals""GetHashCode""GetType""ToString"))  
  15.     } | select "Name"  
  16.     for ($i = 0; $i - lt $rows.Count; $i++) {  
  17.         $Email = $UserData[$i].($ColumnName[0].Name)  
  18.         Write - Host "Updating data for $Email"  
  19.         for ($j = 1; $j - lt $ColumnName.Count; $j++) {  
  20.             $value = $UserData[$i].($ColumnName[$j].Name)  
  21.             if (($value.Length - ge 3) - and(($value.Substring(0, 3) - eq "i:0") - or($value.SubString(0, 3) - eq "c:0"))) {  
  22.                 Set - PnPUserProfileProperty - Account $Email - PropertyName $ColumnName[$j].Name - Values $value - ErrorAction SilentlyContinue  
  23.             } else {#  
  24.                 split the string using the | as a delimiter and load the values into the field.  
  25.                 Set - PnPUserProfileProperty - Account $Email - PropertyName $ColumnName[$j].Name - Values $value.Split("|") - ErrorAction SilentlyContinue  
  26.             }  
  27.             if ($ ? ) {  
  28.                 Write - Host " Set $($ColumnName[$j].Name) --> $($UserData[$i].($ColumnName[$j].Name))." - ForegroundColor Green  
  29.             } else {  
  30.                 Write - Host " Could not set $($ColumnName[$j].Name) --> $($UserData[$i].($ColumnName[$j].Name)). $($error[0].Exception.message)" - ForegroundColor Red  
  31.             }  
  32.         }  
  33.     }  
  34.     $wshell.Popup("Operation Completed!", 0, "Done", 0x1)  
  35. }  

That's it. Now, you can use this script and update SharePoint User Profile properties.

Hope you have learned to update user profile properties for multiple users programmatically using PnP PowerShell scripting. The operations mentioned above are tested on SharePoint Online environment. Feel free to fill up the comment box below, if you need any assistance.