1

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? Commented Jan 25, 2016 at 13:29
  • 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. Commented Jan 25, 2016 at 22:22

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I have added my original code to the question. I modified my code as you suggested and it definitely runs faster. It's now taking close to 30 minutes vs. over 1.5 hours with the original code. Can I have the Get-User command return only the fields that I am interested in? That ought to improve the performance even more no?
Thanks for accepting. How long does it take to run now?
it's now taking 20 - 25 minutes to run; a significant improvement. Thanks for the help.
Sweet. Thanks for sharing. I've also added your sample to the book here: sitecorepowershell.gitbooks.io/sitecore-powershell-extensions/… . You should also remove -Append from my sample since it will keep adding to the existing file.
2

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*"} 

3 Comments

I see a few issues with your examples. 1) ResultSize is not a property available on the command. 2) Age is a custom property and should be accessed from the profile.
@sqone2 That's not the Get-User cmdlet in question - this is

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.