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. It enables My Sites, social features and share profiles across multiple sites in the tenant. To get all user profiles  in SharePoint Online:
  • Login to SharePoint Online Admin >> Click on "More Features" in left navigation >> Click on "Open" button next to "User Profiles".
    powershell to get all user profiles in sharepoint online
  • In User Profiles page, Click on "Manage User Profiles" under People tab. Use Search to get the user profile of the user.SharePoint online get all user profiles powershell
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 profiles in SharePoint Online using PowerShell
#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.
sharepoint online get all user profiles powershell

Here is my other post to get all user profile properties: SharePoint Online: PowerShell to Get User Profile Properties

5 comments:

  1. Thanks for the blog, the questions can you run this script where sharepoint is not installed, i see your are pointing to dll file.

    ReplyDelete
  2. https://www.microsoft.com/en-gb/download/details.aspx?id=42038 from this link install client components

    ReplyDelete
  3. Hello Salauddin,

    Keep up the great job. Your scripts are awesome on which Incan trust blindly. Thank you!!

    ReplyDelete
  4. Thank you for this script. i appreciate your efforts!!

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.