Performance reading large array >40K entries


I’m trying to find the fastest way to search a large array (40K entries) for a value. However, I’m struggling with the way in which the array is working.

I read in all AD users as shown below

$AllADUsers = Get-ADUser -Filter "*" -properties SAMAccountName, DisplayName, UserPrincipalName, Company, Office,Department, Manager, Description, Created, LastLogonDate, EmployeeType,Info
-Server $ADServer -Credential $c
|select SAMAccountName, DisplayName, UserPrincipalName, Company, Office, Department, Manager,
Description, Created, LastLogonDate, EmployeeType,Info
Now this is why I have a query

If I search the array using the .Contains method it finds the entry in around 2ms

But if I then try to pull the data for the record using the where method it takes 1 second
$AllADUsers.Where({$_.UserPrincipalName -eq "$SearchID"})
So why can it find the value in 2ms using contains but then take more than 1 second to read the actual record. Its as if it is using a different search algorithm in the where which takes longer.

In fact it was faster to use Get-AdUser for each search using on 900ms than to pull it from the array.

I’ve also tried other variations to no avail, such as:

$AllADUsers | where-object {$_.UserPrincipalName -eq $SearchID}

Any help gratefully received.




Why are you pulling all entries just to get individual results? You are loading 40k objects into memory to search for a single item. Why not just get what you need?

Get-ADUser -Filter {UserPrincipalName -eq $SearchID}

Active Directory most likely is indexing common unique search items for more efficient searching. When you pull this into memory and search this way, you are doing a plain search with no efficiency and are at the mercy of these filters.


I’m doing an Azure audit on AAD and pulling out additional information to supplement the report as AAD does not hold all the properties we need. So I am reading 1000’s of accounts and mine was just an example.

In my original report I issues a get-aduser for each user I wanted to get additional properties for but this was taking around 900ms per call. I thought it would be quicker to read all users into an array and then read from there.


What is wrong with the formatting on this site!

It’s chopping off sentences…


I have switched to using a HASH table which fixes the overall performance.