Thursday, September 3, 2015

Get and Export User Profile Properties using PowerShell in SharePoint 2013

Its a common requirement in SharePoint to prepare reports from user profile properties. Here is my PowerShell scripts to get user profile properties in SharePoint.

Important: Make sure User Profile Service Application is created , up and running before running these scripts! and run as Farm Administrator!!

Get user profile Properties in SharePoint 2013 using PowerShell
#Configuration Variables
$SiteURL = "http://mysite.crescent.com"
$UserLogin="Crescent\Salaudeen"

#Get Objects
$ServiceContext  = Get-SPServiceContext -site $SiteURL
$UserProfileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext)

#Get the User Profile
$UserProfile = $UserProfileManager.GetUserProfile($UserLogin) 

#Retrieve User Profile Properties
Write-host $UserProfile["PreferredName"]
Write-host $UserProfile["WorkEmail"]

You are not limited to these fields, But there are plenty of more fields in User Profiles. To get all available user profile properties, use:
$UserProfile.Properties | Select DisplayName, Name

PowerShell to Query & Export user profile properties of all users in SharePoint:
Lets use PowerShell to get and export user profile properties of all users.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration Variables
$SiteURL = "http://portal.crescent.com"
$outputReport = "c:\user_profiles.csv"

#Get Objects
$ServiceContext  = Get-SPServiceContext -site $SiteURL
$UPM = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext)

#Get All User Profiles
$UserProfiles = $UPM.GetEnumerator()
Write-host "Total Number of User Profiles found:"$UserProfiles.Count

#Array to hold Profiles
$ProfileDataCollection = @() 

#Iterate through each profile
foreach ($Profile in $UserProfiles)
{
    $ProfileData = New-Object PSObject 
    #Retrieve User profile Properties  
    $ProfileData | Add-Member -MemberType NoteProperty -name "Account" -value $Profile["AccountName"]
    $ProfileData | Add-Member -MemberType NoteProperty -name "Display Name" -value $Profile["PreferredName"]
    $ProfileData | Add-Member -MemberType NoteProperty -name "E-Mail" -value $Profile["WorkEmail"]
    $ProfileData | Add-Member -MemberType NoteProperty -name "Manager" -value $Profile["Manager"]
    $ProfileData | Add-Member -MemberType NoteProperty -name "Department" -value $Profile["Department"]
   
    #Add to Array
    $ProfileDataCollection+=$ProfileData
    write-host "Processed Profile:"$profile["PreferredName"]
}
#Export User Profile data to CSV
$ProfileDataCollection | Export-Csv $outputReport –NoType

This script exports given user profile properties of all users as CSV file:
get user profile powershell sharepoint 2013



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

2 comments :

  1. if i want the output in a sharepoint list with 5 columns (firstname, lastname, workemail, jobtitle, workphone), where i need to make changes

    ReplyDelete
    Replies
    1. Replace the below lines between Line#: 23 to 27.
      $ProfileData | Add-Member -MemberType NoteProperty -name "Account" -value $Profile["FirstName"]
      $ProfileData | Add-Member -MemberType NoteProperty -name "Display Name" -value $Profile["LastName"]
      $ProfileData | Add-Member -MemberType NoteProperty -name "E-Mail" -value $Profile["WorkEmail"]
      $ProfileData | Add-Member -MemberType NoteProperty -name "Manager" -value $Profile["Title"]
      $ProfileData | Add-Member -MemberType NoteProperty -name "Department" -value $Profile["Department"]

      Delete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...