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,
- Install-Module SharePointPnPPowerShellOnline -Scope CurrentUser
Install AzureAD Module
- Install-Module AzureAD -Scope CurrentUser
PowerShell Script
Once we have installed all the dependencies we will now do two operations:
- Connect to Azure AD and fetch all the users present in the Azure AD
- 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:
- Admin Site Collection URL - Provide SharePoint Online Admin Site Collection URL in the format https://<<domain>>-admin.sharepoint.com
- File Path - Please provide the file path along with the file name with .csv as the file extension eg:- E:\Scripts\AllUsersProfiles.csv
- $AdminSiteCollectionUrl = Read-Host "Please enter admin Site Collection URL"
- $FilePath = Read-Host "Please enter the file path along with filename.csv"
-
- $Cred = Get-Credential
-
- Connect-AzureAD -Credential $Cred
- $AzureADUsers = Get-AzureADUser -All:$True
-
- Connect-PnPOnline $AdminSiteCollectionUrl -Credentials $Cred
- $UserProfileData = @()
-
- ForEach($AzureADUser in $AzureADUsers)
- {
- $UserDetail = Get-PnPUserProfileProperty -Account $AzureADUser.UserPrincipalName
- $EachUserProfile = New-Object PSObject
- ForEach($Key in $UserDetail.UserProfileProperties.Keys)
- {
- $EachUserProfile | Add-Member NoteProperty $Key($UserDetail.UserProfileProperties[$Key])
- }
- $UserProfileData += $EachUserProfile
- }
-
- $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.