Fetch All The Users And Their User Profile Properties Using PnP PowerShell

Introduction

 
With so many  companies preferring Microsoft 365 as the platform for their internal collaboration and other uses, it becomes very important to check which users are present and fetch their profile properties from SharePoint.

To achieve this we will use PnP PowerShell and AzureAD PowerShell Modules to fetch all the users and extract their user profile properties from SharePoint
 
Prerequisites
 
If we are using Window 10 or we have  PowerShellGet then we can run the below commands to install PnP PowerShell and AzuerAD Modules
 
Install PnP PowerShell Module, 
  1. Install-Module SharePointPnPPowerShellOnline -Scope CurrentUser  
Install AzureAD Module
  1. Install-Module AzureAD -Scope CurrentUser  

PowerShell Script

 
Once we have installed all the dependencies  we will now do two operations:
  1. Connect to Azure AD and fetch all the users present in the Azure AD
  2. Iterate through all the users and fetch user profile properties for each user
To run the below PowerShell script we should have admin access to fetch all the users and fetch the user profile properties.
 
Two inputs will be  required:
  1. Admin Site Collection URL - Provide SharePoint Online Admin Site Collection URL in the format https://<<domain>>-admin.sharepoint.com
  2. File Path - Please provide the file path along with the file name with .csv as the file extension eg:- E:\Scripts\AllUsersProfiles.csv
  1. $AdminSiteCollectionUrl = Read-Host "Please enter admin Site Collection URL"  
  2. $FilePath = Read-Host "Please enter the file path along with filename.csv"  
  3.   
  4. $Cred = Get-Credential  
  5.   
  6. Connect-AzureAD -Credential $Cred  
  7. $AzureADUsers = Get-AzureADUser -All:$True  
  8.   
  9. Connect-PnPOnline $AdminSiteCollectionUrl -Credentials $Cred  
  10. $UserProfileData = @()  
  11.   
  12. ForEach($AzureADUser in $AzureADUsers)  
  13. {      
  14.     $UserDetail = Get-PnPUserProfileProperty -Account $AzureADUser.UserPrincipalName  
  15.     $EachUserProfile = New-Object PSObject  
  16.     ForEach($Key in $UserDetail.UserProfileProperties.Keys)  
  17.     {     
  18.         $EachUserProfile | Add-Member NoteProperty $Key($UserDetail.UserProfileProperties[$Key])  
  19.     }  
  20.     $UserProfileData += $EachUserProfile  
  21. }  
  22.   
  23. $UserProfileData | Export-Csv -Path $FilePath -NoTypeInformation  

Outcome

 
A CSV file with all the user profile properties will be generated in the specified path. We can use this file for additional analytics.
 
Fetch All The Users And Their User Profile Properties Using PnP Powershell