1
votes

We have over 200k users in our sitecore system. I need to export a list of users who meet a certain criteria. I am using a powershell script for this and I am using the get-user command to retrieve the users. I am then looping through the list of users and picking those that meet my criteria; in my case those users who are older than 18. I am then writing the result out to a csv file using export-csv. I find that this is taking over 1.5 hours to complete.

My question is, is there a way I can do a get-user and specify my criteria of age older than 18? The field age is stored in a custom property. Also, any other efficient ways (other than powershell) of accomplishing what I am trying to do here?

Here is the original code:

function export($user)
{
    $age = $user.profile.GetCustomProperty("age")

    if{$age -gt 18)
    {
        $id = $user.profile.GetCustomProperty("id")
        $firstname = $user.profile.GetCustomProperty("first name")

        $user | select-object -property @{Name="First Name";Expression={$firstname}}, 
            @{Name="Age";Expression={$age}}, 
            @{Name="ID";Expression={$id}} |
        Export-CSV -Path c:\temp\out.csv -Append -NoTypeInformation
    }
}

$users = get-user -Filter * 

if($users -ne $null)
{
    $users | foreach {export($_)}
}
2
Could you provide an example script showing what you doing now to query the users and filter? - Coding101
Thanks for the code. I updated my response with an example that should work for you. The keys are case sensitive for the custom properties. - Coding101

2 Answers

3
votes

Update:

Based on your example I can see why it takes so long. You are exporting to CSV for every iteration.

Try this:

$users = Get-User -Filter * | Where-Object { $_.Profile.GetCustomProperty("age") -gt 18 } 

$property = @(
    "Name",
    @{Name="First Name";Expression={$PSItem.Profile.GetCustomProperty("first name")}}, 
    @{Name="Age";Expression={$PSItem.Profile.GetCustomProperty("age")}}, 
    @{Name="ID";Expression={$PSItem.Profile.GetCustomProperty("id")}}
)
$users | Select-Object -Property $property | Export-CSV -Path c:\temp\out.csv -Append -NoTypeInformation

Old comments:

The more I look at it I'm doubting that this can be done. The age property should be serialized and stored on the profile. Unless there is a faster way to extract the profile date, I'm not sure of what else can be done to speed things up.

I suspect you are doing something like this:

Get-User -Filter * | Where-Object { $_.Profile.GetCustomProperty("Age") -gt 18 }

I don't know a faster way than this.

2
votes

I am then looping through the list of users and picking those that meet my criteria

You should not be doing this. Instead filter the users directly with Get-User

Get-User -Filter * -ResultSize Unlimited | Where-Object {$_.age -ge 18}

As another example, I'll filter users who are over 18 and whose first name begins with "Ste".

Get-User -Filter * -ResultSize Unlimited | Where-Object {$_.age -ge 18 -and $_.FirstName -like "Ste*"}