I need to get all the user profiles that based on a certain user property. Eg "Get all the user that have a property of Department in Finance."

$url = "mysite"
$site = Get-SPSite $url
$context = Get-SPServiceContext $site
$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context)
$profiles = $profileManager.GetEnumerator()

while ($profiles.MoveNext()) {
  $userProfile = $profiles.Current
  $name = $userProfile.DisplayName
  $myprop = $UserProfile["Department"]
  Write-Host "$name;$myprop"

I got an answer on my question. I will retrieve all users and filter the accordingly. here is the script below.

$siteUrl = "http://YOUR_HOSTNAME_HERE"
$outputFile = "c:\temp\sharepoint_user_profiles.csv"

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$serviceContext = Get-SPServiceContext -Site $siteUrl
$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($serviceContext);
$profiles = $profileManager.GetEnumerator()

$fields = @(

$collection = @()

foreach ($profile in $profiles) {
   $user = "" | select $fields
   foreach ($field in $fields) {
     if($profile[$field].Property.IsMultivalued) {
       $user.$field = $profile[$field] -join "|"
     } else {
       $user.$field = $profile[$field].Value
   $collection += $user

$collection | Export-Csv $outputFile -NoTypeInformation
$collection |  Out-GridView
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.