PowerShell to update SharePoint Online User Profile property from Azure AD

Hi Friends, in this post, I am gonna walk you through the steps to update the SharePoint Online User Profile property from Azure AD. You may think, why this is necessary since SharePoint online will sync automatically with Azure AD.

The tricky part is the auto-sync may take up to 24 hrs to sync Azure AD to SharePoint UPS. The next tricky part is not all the AAD profile properties are synced to SharePoint UPS. Only a few of the properties are synced and below are the list of properties relevant to the profile that is synced to SharePoint UPS

  • User Name
  • First name
  • Last name
  • Name
  • Office phone
  • Department
  • Job title
  • Manager
  • Office
  • Office phone

What about the other properties like 'Mobile Phone' that are relevant to the user???

What about if there is a drastic change in the Organization structure and you want the SharePoint UPS to reflect immediately???

To resolve all these obstacles and issues, we need to use PowerShell

Now you understand why there is a need for PowerShell and let me show you how you can make use of some available cmdlets or commands using PowerShell to achieve your goal.

There are 2 different types of PowerShell Modules available to work with SharePoint Online.
  1. SharePoint Online Management Shell
  2. PnP PowerShell
Both the above-mentioned modules can be used to update the User Profile property, but let's talk about the simple that makes our life easier. 'PnP PowerShell' commands are very easy compare to the other.

I think I had bored you with lots of theory, let's jump into some coding stuff.
  • Make sure you installed the required module. If you are not sure whether the module is installed or not, use the below command to check.
Get-Module SharePointPnPPowerShell* -ListAvailable| Select-Object Name,Version| Sort-Object Version -Descending
  • If you are sure that the above command returned nothing, then use the below command to install the module
Install-Module SharePointPnPPowerShellOnline
  • If the module is already installed, you can install the latest version of the module along with the older version or update the existing module to a higher version
Update-Module SharePointPnPPowerShell*
  • We got the module for SharePoint, but what about Azure AD? how are we going to retrieve the data from Azure AD? no worries, we have to do the same for Azure AD module too
Install-Module AzureAD

All the required modules are installed, now let's start communicating with Azure AD to get the information. Following are the steps required to communicate with Azure AD
  • Connect to your tenant Azure AD using the below command. You will be prompted with window to enter the credentials. Enter the credentials and click sign in.
Connect-AzureAD
  • Get the information of the user that you need to update SharePoint. Use the following command to retrieve the user information from Azure AD using the user email or userprincipalname.
$aaduser = Get-AzureADUser -ObjectId "email or userprincipalname"
  • Now all the information relevant to the user is stored in a variable '$aaduser'. When you type n the variable and kept dot, you will see all the properties of Azure AD.

  • Now connect to SharePoint online using the below command.
Connect-PnPOnline –Url https://-admin.sharepoint.com
  • Once you are connected to the admin site, you can use the below command to update the User Profile property
if('' -ne $aaduser.Mobile -and $null -ne $aaduser.Mobile) {
    Set-PnPUserProfileProperty -Account $aaduser.UserPrincipalName -Property 'CellPhone' -Value        $aaduser.Mobile
}
  • The command Set-PnPUserProfileProperty is used to update the profile property of a user. The above command will check the AD user property mobile and if it is not empty then it will update the CellPhone property SharePoint User Profile Store to Azure AD Mobile.
  • To update all the users from Azure AD to SharePoint User Profile Store, use the following command to get all the users and use 'for loop' in PowerShell to iterate and then update the UPS.
$aadusers = Get-AzureADUser -All $true

Happy coding. Cheers 😃

Comments

Popular posts from this blog

SPFx - Office UI Fabric react DetailsList & PropertyFieldCodeEditor to show the CSV data

SPFx - Office UI Fabric react DetailsList & PropertyFieldCodeEditor to show the JSON data