How to I limit the size of the result of a Get-Aduser and process the remaining objects in defined increments?

Hi,

We have a Domain with about 150K User accounts.  Our goal is to produce a list of candidate users which we will then modify attributes on.  Based on the filter used on a Get-Aduser, we may have 50K user accounts we may need to process and set/replace attributes on.  However, we only want to set/replace the attributes on say 10K at one time in order to minimize impact.

I've looked at ResultSetSize, but that only helps determine how many objects to return in my query.  We want to return the entire candidate list of users, then process in defined increments in the future.

Right now, my Get-Aduser looks like the following, then the variable with all the objects gets processed by a Set-Aduser where I set/replace the attributes.  So how do I save all of my $AllCandidateUsers objects, then process them in defined increments in the future?  So I'd like to process the first 10k, then have some way to pick up at object 10,001 and process to 20,000 at a later time, then pick up at object 20,001 and process to 30,000 at a later time, etc.  I'm hoping this can be done programmatically without me keeping track of the objects manually.

$AllCandidateUsers = Get-ADuser -Filter .......

August 25th, 2015 8:19pm

You can use a For loop and refer to elements of the collection by an index variable. Something similar to below (not tested).

$Users = Get-ADUser -Filter ....
For ($k = 1; $k -le 10000; $k =$k + 1)
{
    Set-ADUser -Identity $Users[$k].sAMAccountName ....
}

# Later

$Users = Get-ADUser -Filter ....
For ($k = 10001; $k -le 20000; $k =$k + 1)
{
    Set-ADUser -Identity $Users[$k].sAMAccountName ....
}
Edit: OK, this retrieves all of the objects at once. I don't know of a way to just retrieve the first 10,000, and even if there were a way, there is no guarantee that the next time you query the object number 10,001 will be the same. You need to keep the script running. You can insert a Get-Host to halt the script between groups of 10,000.
Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 10:02pm

Thanks for the response Richard.

So would a better approach be to perform the Get-Aduser and produce the large candidate collection, export to a CSV file, then split the CSV into a manageable number of files, say 4 or 5, then run the smaller files with my Set-Aduser?  The export-csv would give me a 'point in time' candidate list and splitting the large file into smaller ones would give me the 'hard' begin and end of accounts run through the Set-Aduser.

August 25th, 2015 10:47pm

Exporting to a csv is a good idea. You may just need the sAMAccountNames of the users.
Free Windows Admin Tool Kit Click here and download it now
August 26th, 2015 9:15am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics