SharePoint Online: PowerShell to Get All User Profiles
Requirement: Get All User Profiles in SharePoint Online using PowerShell.
How to Get User Profiles in SharePoint Online?
User Profiles in SharePoint is a central location that stores information about users, provides a site membership, and tracks usage data. It enables My Sites, social features, and share profiles across multiple sites in the tenant. To get all user profiles in SharePoint Online, do the following:
- Login to SharePoint Online Admin >> Click on “More Features” in left navigation >> Click on the “Open” button next to “User Profiles”.
- In User Profiles page, click on “Manage User Profiles” under People tab. Use Search to get the user profile of the user.
This page helps us to get individual user profiles and properties one by one. Now, let’s see how to get all user profiles in SharePoint Online using PowerShell.
SharePoint Online: PowerShell to Get All User Profiles
Let’s get all user profile data and reports in SharePoint Online using PowerShell. Make sure you have Azure AD PowerShell Module installed before executing this script.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll"
#Variables
$AdminSiteUrl = "https://crescent-admin.sharepoint.com"
$CSVPath = "C:\Temp\UserProfiles.csv"
#Get Credentials
$Cred = Get-Credential
#Connect to AzureAD
Connect-AzureAD -Credential $Cred
#Get All Users from AzureAD
$AllUsers = Get-AzureADUser -All:$True
Write-host "Total Number of User Profiles Found:"$AllUsers.Count
#Setup the context
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminSiteURL)
$Ctx.Credentials = $Credentials
#Collect User data
$PeopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($Ctx)
$UserDataCollection = @()
$Counter = 1
ForEach($User in $AllUsers)
{
#Get User Profile
$UserProfile = $PeopleManager.GetPropertiesFor(('i:0#.f|membership|'+$User.UserPrincipalName))
$Ctx.Load($UserProfile)
$Ctx.ExecuteQuery()
Write-Progress -Activity "Extracting User Profile Data..." -Status "Getting User Profile $Counter of $($AllUsers.Count)" -PercentComplete (($Counter / $AllUsers.Count) * 100)
#Get User Profile Data
$UserData = New-Object PSObject
ForEach($Key in $UserProfile.UserProfileProperties.Keys)
{
$UserData | Add-Member NoteProperty $Key($UserProfile.UserProfileProperties[$Key])
}
$UserDataCollection += $UserData
$Counter++
}
#Export Data to CSV File
$UserDataCollection | Export-Csv -Path $CSVPath -NoTypeInformation
This PowerShell gets all user profiles and properties in SharePoint Online.
Export User Profile Property Value of All Users using PnP PowerShell
This PowerShell script gets the “Office” property value from all user profiles using the Get-PnPUserProfileProperty cmdlet and exports it to a CSV file:
#Config Variables
$AdminSiteURL = "https://crescent-admin.sharepoint.com"
$SPOPropertyName = "Office"
$CSVPath = "C:\Temp\UserProfileProperty.csv"
Try {
#Connect to AzureAD
Connect-AzureAD
#Get All Users of the Domain from AzureAD
$AllUsers = Get-AzureADUser -All:$True -Filter "UserType eq 'Member'"
Write-host "Total Number of User Profiles Found:"$AllUsers.Count
#Connect to PnP Online
Connect-PnPOnline -Url $AdminSiteURL -Interactive
#Iterate through All Users
$Counter = 1
$UserProfileData = @()
ForEach($User in $AllUsers)
{
Write-host "`nGetting User Profile Property for: $($User.UserPrincipalName)" -f Yellow
#Get the User Property value from SharePoint
$UserProfile = Get-PnPUserProfileProperty -Account ($User.UserPrincipalName)
#Collect the data
$UserProfileData += New-Object PSObject -Property @{
'User Account' = $UserProfile.UserProfileProperties["UserName"]
'Location' = $UserProfile.UserProfileProperties[$SPOPropertyName]
}
$Counter++
Write-Progress -Activity "Getting User Profile Data..." -Status "Getting User Profile $Counter of $($AllUsers.Count)" -PercentComplete (($Counter / $AllUsers.Count) * 100)
}
#Export the data to CSV
$UserProfileData | Export-Csv $CSVPath -NoTypeInformation
write-host -f Green "User Profiles Data Exported Successfully to:" $CSVPath
}
Catch {
write-host -f Red "Error Getting User Profile Property!" $_.Exception.Message
}
In summary, using PowerShell script to retrieve a list of all user profiles in SharePoint Online is a powerful and efficient way to manage and organize user information within an organization. By using PowerShell, you can easily retrieve a list of all user profiles from your SharePoint Online tenant. You can use Microsoft Excel to further analyze, sort, and Filter the user profile data.
Here is my other post to get all user profile properties: SharePoint Online: PowerShell to Get User Profile Properties
Thank you for this script. i appreciate your efforts!!
Hello Salauddin,
Keep up the great job. Your scripts are awesome on which Incan trust blindly. Thank you!!
https://www.microsoft.com/en-gb/download/details.aspx?id=42038 from this link install client components
Thanks for the blog, the questions can you run this script where sharepoint is not installed, i see your are pointing to dll file.
Install SharePoint Online PowerShell Module! How to Install SharePoint Online PowerShell Module?