SharePoint Online: Get User Profile Properties using PowerShell
Requirement: SharePoint Online Get All User Profiles and export to CSV.
How to Get User Profile Properties in SharePoint Online?
In this blog post, we will take a look at how to get user profile properties in SharePoint Online. We will be looking at how to use PowerShell for SharePoint Online, retrieve the properties of a user profile, and then use the SharePoint Admin center to get the profile properties of a specific user. We will also look at some example scripts that retrieve either a particular property or all properties of a given user profile.
A user profile property is a field that holds user data such as “First Name”, “Last Name”, “Department”, “Skills”, etc. By default, SharePoint already has a rich set of user profile properties. To get all user profiles and user profile properties in SharePoint Online, follow these steps:
- Login to SharePoint Online Admin Center >> Click on the “More Features” link from the left navigation >> Click on the “Open” button under “User Profiles”.
- In User Profiles, click on the “Manage User Profiles” link under the People tab.
- Use Search to get the user profile of the user >> Choose “Edit My Profile” from the context menu.
This interface helps us to get individual user profiles and properties one by one. Now, let’s see how to get user profile properties in SharePoint Online using PowerShell.
SharePoint Online PowerShell to Get User Profile Properties
Here is the PowerShell to get all user profiles in SharePoint Online. It gets the specific property “Department” of the particular user from the given site collection.
#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 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="[email protected]"
$Property="Department"
Get-SPOUserProfileProperty -AdminSiteURL $AdminSiteURL -UserAccount $UserAccount -Property $Property
This PowerShell gets the particular property of a specific user. Similarly, you can get any available user property such as Email, Manager, etc., from the SharePoint Online user profile using PowerShell.
SharePoint Online: Get User Profile Properties using PowerShell
You can also get user profile properties using the User Profile Web Service. Here is how:
#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"
#variables
$AdminCenterURL = "https://Crescent-admin.sharepoint.com"
$SiteURL="https://Crescent.sharepoint.com/"
#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Authenticate the web service
$UserProfileService= New-WebServiceProxy -Uri ("$AdminCenterURL/_vti_bin/UserProfileService.asmx?wsdl") -UseDefaultCredential False
$UserProfileService.Credentials = $Credentials
$URI = New-Object System.Uri($AdminCenterURL)
$Container = New-Object System.Net.CookieContainer
$Container.SetCookies($URI, $Credentials.GetAuthenticationCookie($AdminCenterURL))
$UserProfileService.CookieContainer = $Container
#Sets the first User profile, at index -1
$UserProfileResult = $UserProfileService.GetUserProfileByIndex(-1)
#To Get All Properties available: $UserProfileResult.UserProfile| Select name
#Loop through all user profiles
While ($UserProfileResult.NextValue -ne -1)
{
#Get the user UPN property
$Property = $UserProfileResult.UserProfile | Where-Object { $_.Name -eq "SPS-UserPrincipalName"}
Write-host $Property.Values[0].Value
#Get Account Name
$Property = $UserProfileResult.UserProfile | Where-Object { $_.Name -eq "AccountName"}
Write-host $Property.Values[0].Value
#Get the next profile
$UserProfileResult = $UserProfileService.GetUserProfileByIndex($UserProfileResult.NextValue)
}
PnP PowerShell to Get User Profile Properties in SharePoint Online
Here is how to get all user profile properties in SharePoint Online using the PnP PowerShell cmdlet Get-PnPUserProfileProperty:
#Set Variables
$SiteURL = "https://crescent.sharepoint.com"
$UserAccount = "[email protected]"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get all properties of a User Profile
$UserProfile = Get-PnPUserProfileProperty -Account $UserAccount
$UserProfile.UserProfileProperties
This script retrieves all properties from a user profile.
Property Name |
---|
UserProfile_GUID |
SID |
ADGuid |
AccountName |
FirstName |
SPS-PhoneticFirstName |
LastName |
SPS-PhoneticLastName |
PreferredName |
SPS-PhoneticDisplayName |
WorkPhone |
Department |
Title |
SPS-Department |
Manager |
AboutMe |
PersonalSpace |
PictureURL |
UserName |
QuickLinks |
WebSite |
PublicSiteRedirect |
SPS-JobTitle |
SPS-DataSource |
SPS-MemberOf |
SPS-Dotted-line |
SPS-Peers |
SPS-Responsibility |
SPS-SipAddress |
SPS-MySiteUpgrade |
SPS-DontSuggestList |
SPS-ProxyAddresses |
SPS-HireDate |
SPS-DisplayOrder |
SPS-ClaimID |
SPS-ClaimProviderID |
SPS-LastColleagueAdded |
SPS-OWAUrl |
SPS-ResourceSID |
SPS-ResourceAccountName |
SPS-MasterAccountName |
SPS-UserPrincipalName |
SPS-O15FirstRunExperience |
SPS-PersonalSiteInstantiationState |
SPS-DistinguishedName |
SPS-SourceObjectDN |
SPS-LastKeywordAdded |
SPS-ClaimProviderType |
SPS-SavedAccountName |
SPS-SavedSID |
SPS-ObjectExists |
SPS-PersonalSiteCapabilities |
SPS-PersonalSiteFirstCreationTime |
SPS-PersonalSiteLastCreationTime |
SPS-PersonalSiteNumberOfRetries |
SPS-PersonalSiteFirstCreationError |
SPS-FeedIdentifier |
WorkEmail |
CellPhone |
Fax |
HomePhone |
Office |
SPS-Location |
Assistant |
SPS-PastProjects |
SPS-Skills |
SPS-School |
SPS-Birthday |
SPS-StatusNotes |
SPS-Interests |
SPS-HashTags |
SPS-EmailOptin |
SPS-PrivacyPeople |
SPS-PrivacyActivity |
SPS-PictureTimestamp |
SPS-PicturePlaceholderState |
SPS-PictureExchangeSyncState |
SPS-MUILanguages |
SPS-ContentLanguages |
SPS-TimeZone |
SPS-RegionalSettings-FollowWeb |
SPS-Locale |
SPS-CalendarType |
SPS-AltCalendarType |
SPS-AdjustHijriDays |
SPS-ShowWeeks |
SPS-WorkDays |
SPS-WorkDayStartHour |
SPS-WorkDayEndHour |
SPS-Time24 |
SPS-FirstDayOfWeek |
SPS-FirstWeekOfYear |
SPS-RegionalSettings-Initialized |
OfficeGraphEnabled |
SPS-UserType |
SPS-HideFromAddressLists |
SPS-RecipientTypeDetails |
DelveFlags |
VideoUserPopup |
PulseMRUPeople |
msOnline-ObjectId |
SPS-PointPublishingUrl |
SPS-TenantInstanceId |
SPS-SharePointHomeExperienceState |
SPS-RefreshToken |
SPS-MultiGeoFlags |
PreferredDataLocation |
Country |
You can query a specific property as:
Write-host $UserProfile.UserProfileProperties["Department"]
If you need to get all properties from all user profiles and export them to a CSV file, Here is another post: Export All User Profile Properties in SharePoint Online using PowerShell
I have used the above script in the past, to get list of users and their custom properties from SharePoint Online USer profiles. But now I have been getting ‘Error Exporting User Profile Properties! Exception calling “ExecuteQuery” with “0” argument(s): “The sign-in name or password does not match one in the Microsoft account system.”‘ after enabling 2-factor authentication.
Same issue here…
Does not work with MFA as far as I’ve been able to determine. I’ve resorted to using a non-MFA enabled service account
If you are using MFA, you have to authenticate differently! Connect to SharePoint Online with Multi-factor Authentication (MFA) in PowerShell