SharePoint Online: Export User Profile Properties to CSV using PowerShell
Requirement: Export All User Profiles and Properties from SharePoint Online to a CSV File.
SharePoint Online: Export User Profiles to CSV using PowerShell
User profiles contain a wide range of information about individuals, including their job titles, department, contact information, and more. Sometimes we need to export user profile properties from the SharePoint Online environment. This is very easy with PowerShell, and in this blog post, I will show you how to export the user profile property values from your SharePoint Online to a CSV format.
Here is how to export 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"
Function Export-SPOUserProfileProperties()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $CSVPath
)
Try {
#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Delete the CSV report file if exists
if (Test-Path $CSVPath) { Remove-Item $CSVPath }
#Get all Users
$Users = $Ctx.Web.SiteUsers
$Ctx.Load($Users)
$Ctx.ExecuteQuery()
Write-host "Total Number of Profiles Found:"$Users.count -f Yellow
#Get User Profile Manager
$PeopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($Ctx)
#Array to hold result
$UserProfileData = @()
Foreach ($User in $Users)
{
Write-host "Processing User Name:"$User.LoginName
#Get the User Profile
$UserProfile = $PeopleManager.GetPropertiesFor($User.LoginName)
$Ctx.Load($UserProfile)
$Ctx.ExecuteQuery()
if($UserProfile.Email -ne $Null)
{
#Send Data to object array
$UserProfileData += New-Object PSObject -Property @{
'User Account' = $UserProfile.UserProfileProperties["UserName"]
'Full Name' = $UserProfile.UserProfileProperties["PreferredName"]
'E-mail' = $UserProfile.UserProfileProperties["WorkEmail"]
'Department' = $UserProfile.UserProfileProperties["Department"]
'Location' = $UserProfile.UserProfileProperties["Office"]
'Phone' = $UserProfile.UserProfileProperties["WorkPhone"]
'Job Title' = $UserProfile.UserProfileProperties["Title"]
}
}
}
#Export the data to CSV
$UserProfileData | Export-Csv $CSVPath -Append -NoTypeInformation
write-host -f Green "User Profiles Data Exported Successfully to:" $CSVPath
}
Catch {
write-host -f Red "Error Exporting User Profile Properties!" $_.Exception.Message
}
}
#Call the function
$SiteURL="https://crescent-my.sharepoint.com"
$CSVPath="C:\Temp\UserProfiles.csv"
Export-SPOUserProfileProperties -SiteURL $SiteURL -CSVPath $CSVPath
This PowerShell script gets user profiles from a site collection and exports them to an Excel file (CSV). Well, How do we extract user profiles for all site collections? There is no direct way to connect to the User Profile Service Application in SharePoint Online and get all user profile properties for all users in the tenant using CSOM. Either you’ll have to iterate through all Site Collections (and remove duplicates among them!) or use Web Services to connect to SharePoint Online and retrieve all user profiles.
However, a better way exists: Let’s use the Combination of SharePoint Online CSOM and Azure AD Connect!
Pr-Requisites: Make sure you have SharePoint Online Client SDK (https://www.microsoft.com/en-us/download/details.aspx?id=42038) and Azure Active Directory Module (https://technet.microsoft.com/en-us/library/dn975125.aspx) installed on your client machine, before using this script!
PowerShell to Export All User Profile Properties in SharePoint Online
Rather than getting user profiles belonging to a particular site collection, let us get all user profiles of the tenant and export user profile properties to a CSV file.
#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"
#Import Azure AD Module
Import-Module MSOnline
Function Export-AllUserProfiles()
{
param
(
[Parameter(Mandatory=$true)] [string] $TenantURL,
[Parameter(Mandatory=$true)] [string] $CSVPath
)
Try {
#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($TenantURL)
$Ctx.Credentials = $Credentials
#Delete the CSV report file if exists
if (Test-Path $CSVPath) { Remove-Item $CSVPath }
#Get all Users
Connect-MsolService -Credential $Cred
$Users = Get-MsolUser -All | Select-Object -ExpandProperty UserPrincipalName
Write-host "Total Number of Profiles Found:"$Users.count -f Yellow
#Get User Profile Manager
$PeopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($Ctx)
#Array to hold result
$UserProfileData = @()
Foreach ($User in $Users)
{
Write-host "Processing User Name:"$User
#Get the User Profile
$UserLoginName = "i:0#.f|membership|" + $User #format to claims
$UserProfile = $PeopleManager.GetPropertiesFor($UserLoginName)
$Ctx.Load($UserProfile)
$Ctx.ExecuteQuery()
if($UserProfile.Email -ne $Null)
{
#Send Data to object array
$UserProfileData += New-Object PSObject -Property @{
'User Account' = $UserProfile.UserProfileProperties["UserName"]
'Full Name' = $UserProfile.UserProfileProperties["PreferredName"]
'E-mail' = $UserProfile.UserProfileProperties["WorkEmail"]
'Department' = $UserProfile.UserProfileProperties["Department"]
'Location' = $UserProfile.UserProfileProperties["Office"]
'Phone' = $UserProfile.UserProfileProperties["WorkPhone"]
'Job Title' = $UserProfile.UserProfileProperties["Title"]
}
}
}
#Export the data to CSV
$UserProfileData | Export-Csv $CSVPath -Append -NoTypeInformation
write-host -f Green "User Profiles Data Exported Successfully to:" $CSVPath
}
Catch {
write-host -f Red "Error Exporting User Profile Properties!" $_.Exception.Message
}
}
#Call the function
$TenantURL="https://crescent.sharepoint.com"
$CSVPath="C:\Temp\UserProfiles.csv"
Export-AllUserProfiles -TenantURL $TenantURL -CSVPath $CSVPath
This script extracts all user profiles from SharePoint Online Tenant to a CSV File with the following properties:
- Account
- Full Name
- Department
- Location,
- Job Title
You can add additional properties. Here is the Report output created by the PowerShell Script:
PnP PowerShell to Export All User Profile Properties
Here is the PnP PowerShell to extract all properties of every user profile to a CSV report:
#Config Variables
$AdminSiteURL = "https://crescent-admin.sharepoint.com"
$CSVPath = "C:\Temp\UserProfiles.csv"
Try {
#Connect to AzureAD
Connect-AzureAD | Out-Null
#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 -UseWebLogin
#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)
#Get User Profile Data
$UserData = New-Object PSObject
ForEach($Key in $UserProfile.UserProfileProperties.Keys)
{
$UserData | Add-Member NoteProperty $Key($UserProfile.UserProfileProperties[$Key])
}
$UserProfileData += $UserData
$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
}
Exporting user profile properties from SharePoint Online can be useful for managing and manipulating user data. By following the steps outlined in this article, you can export user profile properties and review the user profile data.
Encountering an error: Error Exporting User Profile Properties! Cannot find an overload for “PeopleManager” and the argument count: “1”.
Confirmed these scripts just work fine. You can probably try downloading and installing the latest version of the CSOM assemblies once. Download and Install SharePoint Online Client Components SDK using PowerShell
Hi Thanks for sharing this scripts
I tried to run the first script “SharePoint Online: Export User Profiles to CSV using PowerShell”, but half way through the console returned the following error message:
Error Exporting User Profile Properties! Exception calling “ExecuteQuery” with “0” argument(s): “The remote server returned an error: (429).”
I searched on google for solutions and one of the first entries came with this suggestion:
“To work around this issue, I suggest you put all the uploading tasks in one console application, schedule the uploading frequency in a certain interval in the code logic, this might help to reduce the burden on both client and server machine.”
https://social.msdn.microsoft.com/Forums/azure/en-US/bc93fed0-5771-4878-8762-bd7cb98926de/the-remote-server-returned-an-error-429-too-many-requests-csom-c?forum=sharepointdevelopment
I’ll try to work out a solution myself, but it will be worth to update your scrip(s)t to overcome this issue.