Requirement: SharePoint Online Get All User Profiles and export to CSV.
How to Get All user Profiles and User Profile Properties in SharePoint Online?
SharePoint Online PowerShell to Get User Profile Properties
This PowerShell gets the specific property "Department" of the particular user from given site collection.
Get All User Profiles in SharePoint Online Site Collection using PowerShell
Here is how to user profile in SharePoint online using PowerShell.
However, There is a better way: Lets 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!
SharePoint Online Get User Profile Properties using PowerShell
Rather getting user profiles belong to a particular site collection, lets get all user profiles of the tenant and export user profile properties to CSV file.
How to Get All user Profiles and User Profile Properties in SharePoint Online?
- Login to SharePoint Online AdminCenter >> Click on "User Profiles" link from left navigation
- In User Profiles, Click on "Manage User Profiles" under People tab. Use Search to get the user profile of the user.
SharePoint Online PowerShell to Get User Profile Properties
This PowerShell gets the specific property "Department" of the particular user from given site collection.
#Load SharePoint CSOM AssembliesThis PowerShell gets the particular property of a specific user. How to get all user profiles of a site collection?
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 Get-SPOUserProfileProperty()
{
param
(
[Parameter(Mandatory=$true)] [string] $AdminSiteURL,
[Parameter(Mandatory=$true)] [string] $UserAccount,
[Parameter(Mandatory=$true)] [string] $Property
)
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($AdminSiteURL)
$Ctx.Credentials = $Credentials
#Get the User
$User = $Ctx.web.EnsureUser($UserAccount)
$Ctx.Load($User)
$Ctx.ExecuteQuery()
#Get User Profile
$PeopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($Ctx)
$UserProfile = $PeopleManager.GetPropertiesFor($User.LoginName)
$Ctx.Load($UserProfile)
$Ctx.ExecuteQuery()
#Get the User Profile Property
Write-host $UserProfile.UserProfileProperties[$Property]
}
Catch {
write-host -f Red "Error Getting User Profile Properties!" $_.Exception.Message
}
}
#Call the function
$AdminSiteURL="https://crescent-admin.sharepoint.com"
$UserAccount="Kacper@crescent.com"
$Property="Department"
Get-SPOUserProfileProperty -AdminSiteURL $AdminSiteURL -UserAccount $UserAccount -Property $Property
Get All User Profiles in SharePoint Online Site Collection using PowerShell
Here is how to user profile in SharePoint online using PowerShell.
#Load SharePoint CSOM AssembliesThis PowerShell script gets user profiles from given site collection and exports them to Excel file (CSV). Well, How do we extract user profiles for all site collections? There is no direct way to connect to 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 iterate through all Site Collections (and remove duplicates among them!) or use Web Services to connect to SharePoint Online and retrieve all user profiles.
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.sharepoint.com"
$CSVPath="C:\Temp\UserProfiles.csv"
Export-SPOUserProfileProperties -SiteURL $SiteURL -CSVPath $CSVPath
However, There is a better way: Lets 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!
SharePoint Online Get User Profile Properties using PowerShell
Rather getting user profiles belong to a particular site collection, lets get all user profiles of the tenant and export user profile properties to CSV file.
#Load SharePoint CSOM AssembliesThis script extracts all user profiles from SharePoint online Tenant to a CSV File with following properties:
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
- Account
- Full Name
- Department
- Location,
- Job Title