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?
A user profile property is the 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:
  • Login to SharePoint Online Admin >> 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 get all user profiles powershell
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 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="Kacper@crescent.com"
$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 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://crescenttech-admin.sharepoint.com"
$SiteURL="https://crescenttech.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)
} 
If you need to get all properties from all user profiles and export them to a CSV file, Here is my another post  Export All User Profile Properties in SharePoint Online using PowerShell

PnP PowerShell to Get User Profile Properties in SharePoint Online
#Set Variables
$SiteURL = "https://crescent.sharepoint.com"
$UserAccount = "Salaudeen@crescent.com"

#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get all properties of a User Profile
$UserProfile = Get-PnPUserProfileProperty -Account $UserAccount
$UserProfile.UserProfileProperties
This script retrieves all properties from a user profile. You can query a specific property as:
Write-host $UserProfile.UserProfileProperties["Department"]
SharePoint Online: Get User Profile Properties using PowerShell SharePoint Online: Get User Profile Properties using PowerShell Reviewed by Salaudeen Rajack on January 24, 2017 Rating: 5

3 comments:

  1. 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.

    ReplyDelete
    Replies
    1. 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

      Delete

Please Login and comment to get your questions answered!

Powered by Blogger.