Export UserProfiles to CSV using Powershell

Often we get asked how to export a specific set of users from the SharePoint UserProfile Service into a CSV file. Mostly all SharePoint Profiles with a specific UserProfile Attribute have to be exported. The following PowerShell script, we would like to share, exports all UserProfiles with an Email address into a CSV file.

I hope that script helps you with similar requests.

Add-PSSnapin *sharepoint*

$url = "URL of SharePoint Site"
$exportFileUrl = $PSScriptRoot + "\export.csv"

$results = @();

$site = Get-SPSite $url
$context = Get-SPServiceContext $site
$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfilemanager($context)
$profiles = $profileManager.GetEnumerator()
$countTotal = $profileManager.Count;
$count = 0;

while ($profiles.MoveNext()) {
  $count++
  Write-Progress -Activity "Reading Userprofiles" -Status "User $count of $countTotal" -PercentComplete ($count / $countTotal * 100)

  $userProfile = $profiles.Current
  $name = $userProfile.DisplayName
  $criteria = $userProfile["WorkEmail"]

  if($criteria -ne ""){
    $match = New-Object PSObject
    $match | Add-Member -MemberType NoteProperty -name "Email" -Value $userProfile["WorkEmail"]
    $match | Add-Member -MemberType NoteProperty -name "AccountName" -Value $userProfile["AccountName"]
    $match | Add-Member -MemberType NoteProperty -name "DisplayName" -Value $userProfile.DisplayName
    $match | Add-Member -MemberType NoteProperty -name "FirstName" -Value $userProfile["FirstName"]
    $match | Add-Member -MemberType NoteProperty -name "LastName" -Value $userProfile["LastName"]
    $match | Add-Member -MemberType NoteProperty -name "Company" -Value $userProfile["Company"]    

    $results += $match;    
  }
}

$results | export-csv $exportFileUrl -NoTypeInformation
0 Kommentare

Dein Kommentar

An Diskussion beteiligen?
Hinterlasse uns Deinen Kommentar!

Schreibe einen Kommentar