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.
- $siteurl="https://<tenant-admin>.sharepoint.com"
- Connect-PnPOnline -Url $siteurl
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.
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.
- $csvFile=’$csvFile = 'F:\Ravishankar\Deployment\UserData.csv'
- $UserData= Import-Csv $csFile
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,
- $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++) {
- $value = $UserData[$i].($ColumnName[$j].Name)
- if (($value.Length - ge 3) - and(($value.Substring(0, 3) - eq "i:0") - or($value.SubString(0, 3) - eq "c:0"))) {
- Set - PnPUserProfileProperty - Account $Email - PropertyName $ColumnName[$j].Name - Values $value - ErrorAction SilentlyContinue
- } else {#
- split the string using the | as a delimiter and load the values into the field.
- Set - PnPUserProfileProperty - 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
- } else {
- Write - Host " Could not set $($ColumnName[$j].Name) --> $($UserData[$i].($ColumnName[$j].Name)). $($error[0].Exception.message)" - ForegroundColor Red
- }
- }
- }
Final code
- $sitecollectionUrl = Read - Host 'Please Enter the Site Collection URL'
- $csvFile = Read - Host 'Please Enter the Path of ur Excel (.csv)'
- $credentials = Get - Credential
- Connect - PnPOnline - Url $sitecollectionUrl - Credentials $credentials
- $wshell = New - Object - ComObject Wscript.Shell
- try {
- $ctx = Get - PnPContext
- } catch {
- $wshell.Popup("Please connect to tenant admin site!", 0, "Done", 0x1)
- }
- if ($ctx) {
- $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++) {
- $value = $UserData[$i].($ColumnName[$j].Name)
- if (($value.Length - ge 3) - and(($value.Substring(0, 3) - eq "i:0") - or($value.SubString(0, 3) - eq "c:0"))) {
- Set - PnPUserProfileProperty - Account $Email - PropertyName $ColumnName[$j].Name - Values $value - ErrorAction SilentlyContinue
- } else {#
- split the string using the | as a delimiter and load the values into the field.
- Set - PnPUserProfileProperty - 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
- } else {
- Write - Host " Could not set $($ColumnName[$j].Name) --> $($UserData[$i].($ColumnName[$j].Name)). $($error[0].Exception.message)" - ForegroundColor Red
- }
- }
- }
- $wshell.Popup("Operation Completed!", 0, "Done", 0x1)
- }
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.