In Focus

PowerShell Script - Synchronize The User Profile Properties From AD To SharePoint Online User Properties

In this blog, I'll share the PowerShell script to synchronize the Office 365 AD properties with SharePoint Online user profile properties.

Recently, I got a chance to write a PowerShell script for syncing the user profile properties from Azure AD to SharePoint Online.

Here, I’ll share a PowerShell script which synchronizes the mobile phone and city.

High-level steps
  1. Get the parameters,

    • Credential File Path- with Username and Password on two different lines.
    • Convert the password into a secure string
    • Admin the site URL.

  2. Import respective libraries.
  3. Connect AzureAD.
  4. Connect SharePoint Online.
  5. Get the instance of PeopleManager.
  6. Fetch all users from AzureAD.
  7. Loop through all AzureAD users.

    • Read the properties which we want to synchronize.
    • Use PeopleManager SetSingleValueProfileProperty() to synchronize the user profile properties in SharePoint Online.
PowerShell Script
  1. <#  
  2. .SYNOPSIS  
  3.  Sync given SPO user profile properties with Azure AD values  
  4.   
  5.  .PARAMETER CredentialFilePath  
  6.   Office 365 system account credential file path having two lines in following format  
  7.   UserName  
  8.   Password  
  9.   
  10.  .PARAMETER SPOAdminURL  
  11.   SharePoint Online Admin Site URL  
  12. #>  
  13.   
  14. param  
  15. (  
  16.     [parameter(Mandatory=$true)][string]$CredentialFilePath,  
  17.     [parameter(Mandatory=$true)][string]$SpoAdminUrl,  
  18.     [parameter(Mandatory=$false)][string]$LogFolderPath = "c:\"  
  19. )  
  20.   
  21.   
  22. if(-not(Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"}))  
  23. {  
  24.     Add-PSSnapin Microsoft.SharePoint.PowerShell;  
  25. }  
  26.   
  27. Import-Module MSOnline  
  28. Import-Module Microsoft.Online.SharePoint.PowerShell  
  29.  
  30. # add SharePoint CSOM libraries on given path  
  31. Import-Module 'C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll'  
  32. Import-Module 'C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'  
  33. Import-Module 'C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll'  
  34.  
  35. #Function to write the log. Put all logs in log.txt  
  36. Function LogWrite  
  37. {  
  38.    Param ([string]$logstring)  
  39.    $Logfile = $LogFolderPath + "\log.txt"  
  40.    Add-content $Logfile -value $logstring  
  41. }  
  42.   
  43.   
  44. Try {  
  45.   
  46.     LogWrite "Syncing the AD Properties"  
  47.  
  48.     #Get the user credential file path and getting user from it  
  49.     $user = Get-Content $CredentialFilePath | Select-Object -First 1  
  50.  
  51.     #Getting password  
  52.     $password = Get-Content $CredentialFilePath | Select-Object -First 1 -Skip 1  
  53.     $securePassword = ConvertTo-SecureString $Password -AsPlainText -Force  
  54.  
  55.     #Credential object  
  56.     $credential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $user, $securePassword  
  57.  
  58.     # Connect to AzureAD  
  59.     Connect-MsolService -Credential $credential  
  60.   
  61.     LogWrite "Azure Connected"  
  62.  
  63.     # Get credentials for SharePointOnline  
  64.     $spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credential.GetNetworkCredential().Username, (ConvertTo-SecureString $credential.GetNetworkCredential().Password -AsPlainText -Force))  
  65.       
  66.     $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SpoAdminUrl)  
  67.     $ctx.Credentials = $spoCredentials  
  68.       
  69.     $spoPeopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($ctx)  
  70.  
  71.     # Get all AzureAD Users  
  72.     $AzureADUsers = Get-MSolUser -All  
  73.      
  74.     #Here, we are also writting the CSV file. Adding headings to CSV file.  
  75.     Add-Content -Path C:\Users.csv  -Value '"MobilePhone","City","Street Address","Country","TargetSPOUserAccount"'  
  76.  
  77.     #looping through all the AD users and getting respective properties which we need to sync  
  78.     ForEach ($AzureADUser in $AzureADUsers) {  
  79.          
  80.         #mobile phone  
  81.         $mobilePhone = $AzureADUser.MobilePhone  
  82.         #city  
  83.         $city = $AzureADUser.City  
  84.  
  85.         #getting the user name  
  86.         $targetUPN = $AzureADUser.UserPrincipalName.ToString()  
  87.         #SPO formatting user  
  88.         $targetSPOUserAccount = ("i:0#.f|membership|" + $targetUPN)  
  89.   
  90.         LogWrite "Synchronising the user - $targetUPN"  
  91.  
  92.         #preparing string to write all users in CSV file   
  93.         $line = $mobilePhone +"," + $city +"," + $streetAddress +"," + $country + "," + $targetSPOUserAccount;  
  94.  
  95.         #writting to CSV file  
  96.         Add-Content -Path C:\Users.csv  -Value $line  
  97.   
  98.         $cellPhone_PropertyName = "CellPhone"  
  99.         $office_PropertyName = "Office"  
  100.   
  101.         $userCellPhone = $targetUserCellPhone.Value  
  102.  
  103.         #SetSingleValueProfileProperty - updating SPO user profile for mobile phone and city  
  104.         $spoPeopleManager.SetSingleValueProfileProperty($targetspoUserAccount, $cellPhone_PropertyName, $mobilePhone)  
  105.         $spoPeopleManager.SetSingleValueProfileProperty($targetspoUserAccount, $office_PropertyName, $city)  
  106.                       
  107.         $ctx.ExecuteQuery()  
  108.     } #foreach  
  109.   
  110.     LogWrite "All users properties are synchronised successfully"  
  111. }  
  112. Catch {  
  113.    [Exception]  
  114.    LogWrite $Error  

References