Help with querying a large number of Active Directory accounts (PowerShell)
I need to query about 14,000 Active Directory accounts via PowerShell and would like your suggestions on which approach would be the most reasonable to use. The methods I've considered are outlined below but feel free to suggest any other ones I may not have thought of.
For each method I would also limit the Get-ADUser cmdlet with the -Properties attribute to only return whichever AD attributes I need (about 7 of them, some standard and some custom). I've omitted that below to keep the example code short.
Method 1: I could fetch all user account objects to a single variable and then get the data I need from there
How concerned should I be about potentially overloading a domain controller if the total number of accounts in the domain is quite large (60,000+)?
$results = Get-ADUser -Filter '*'
Method 2: I could fetch only the 14,000 user accounts I'm interested in by making individual requests for each of them within a loop
In contrast to the first method above, would this be more preferable (lots of small requests versus a single large one)? Again, should I be concerned about the load on a domain controller?
$results = @()
foreach ($username in $usernames) {
$results += Get-ADUser $username
}
Method 3: I could fetch the 14,000 user accounts but group them by for example 10 users per each Get-ADUser cmdlet utilizing the -Filter attribute.
This would add more complexity in terms of preparing these filter statement groupings beforehand, etc. But instead of one large request (for all users) or many small individual requests (14,000 of them) there would be a more reasonable number of requests (about 1,400 assuming each grouping has 10 users). Would this be any better than the methods above?
# ..below would be in a loop going through all the -Filter groups
$results += Get-ADUser -Filter { (SAMAccountName -eq 'User1') -or (SAMAccountName -eq 'User2') -or ... -or (SAMAccountName -eq 'User10') }
For the sake of simplicity I would of course prefer either method 1 or 2. But I'm concerned going against sanity/best practice due to the number of accounts.
EDIT: Thanks for all the comments!! I really appreciate all the different viewpoints and suggestions, and it's great to get affirmation that the scope of this should not put any significant load on the DC. I will likely be going with Method 1, though I'm quite interested in exploring some of the other suggested approaches also (for example the ADSI one and others) if I find time for it.
Posted Jan 3, 2020, 4:57 PM UTC · edited
Just do the first method. This won't even put a dent in a DC's performance.