Categories:

Update SharePoint User Profile Properties for Multiple Users 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 CSV file as the input.

The right way to utilize PowerShell to oversee SharePoint Online is to install SharePoint Online management shell first. 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

sharepoint tenant

Step 2: 

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

The following cmdlets helps you to get Context from the SharePoint site, 

$ctx = Get-PnPContext 

cmdletsStep 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 file. 

$csvFile= $csvFile = ‘F:RavishankarDeploymentUserData.csv’ 

$UserData= Import-Csv $csFile 

ImportCsv

Step 4: 

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

1. Account – Account of the user, either login name or email of the user 
2. PropertyName – Give the Property name, for instance SPS-Location, SPS-Department 
3. 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 

                } 

           } 

    } 

Output-2

Final code : 

$sitecollectionUrl= Read-Host ‘Please Enter the Site Collection URL’ 

$csvFile = Read-Host ‘Please Enter the Path of ur Excel (.csv)’ 

$czedentials= 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 

                {

Sharing is Caring!

Leave a Reply

Your email address will not be published. Required fields are marked *