I have a number of users who need to have some properties of their Profile updated. I've been using UserProfileManager.GetEnumerator to get a collection of all users in the system and am then selecting individual users by their WorkEmail property.

    Add-PSSnapin Microsoft.SharePoint.Powershell -erroraction SilentlyContinue

    $mySiteHostSite = "<siteurl>"
    $context = Get-SPServiceContext $mySiteHostSite
    $profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context)

    $names = @("example1@company.co.nz","example2@company.co.nz")
    $profiles = $profileManager.GetEnumerator()

    foreach($name in $names){
        $profile =  ($profiles | where { $_['WorkEmail'] -like "*$($name)" })

The issue I am having is that each time the loop iterates the $profiles object gets cleared out. I can make this work by replacing:

    $profile =  ($profiles | where { $_['WorkEmail'] -like "*$($name)" 


    $profile =  ($profileManager.GetEnumerator() | where { $_['WorkEmail'] -like "*$($name)" 

however I'd rather not have to repeatedly make this call. Is there a way to ensure that the $profiles collection is maintained through this loop?


I implemented the following solution -

    function loadAllProfiles($profiles){
        foreach($prof in $profiles){
            $profilesArr += $prof

        return $profilesArr

Just looping the collection and populating a separate array. It does still seem odd that making a selection from the GetProfiles object clears out the result but this solution has resolved my issue.

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.