Select users in groups distinct bunches

I would like to go through a large list of users but process them in batches of about 15. For each of the distinct group of 15, they would get a distinct accountexpirationdate e.g., “Set-ADUser -AccountExpirationDate $StartDate.AddDays($j)” where $j is the certain batch of 15. So, for example, I have 1500 total users and I would like to take each 15 and give them an accountexpirationdate of say March 1, 2020 and then the next batch of 15 and increment the accountexpirationdate by one day ie…, March 2, 2020 and so on.

I can do this by placing a for loop going through all 1500 users and then nesting a bunch of if statements (about 100) that would do the trick. I am looking for a more clever and concise bit of scripting.

You should provide some code first, but below is a foreach loop example that will work.

# Outputs every 15th number (end of batch) to console
foreach ($n in (1..1500)){
$a++ ; If ($a -eq 15){Write-Verbose "$n is end of batch" -Verbose ; $a=0}
}

As js said, you should provide some code on what you’ve tried. Something to start with:

#Generate mock users
$users = 1..1500 | foreach{[pscustomobject]@{User="User$_"}}

#Initial Exp Date
$expDate = (Get-Date).AddDays(30)

for ($i = 0;$i -lt $users.Count;$i+=15) {
    if ($i -eq 0) {
        $users | Select User, @{Name='ExpirationDate';Expression={$expDate}} -First 15 
    }
    else {
        $expDate = $expDate.AddDays(1)
        $users | Select User, @{Name='ExpirationDate';Expression={$expDate}} -First 15 -Skip $i
    }
}

@random command line. I don’t quite follow your script. Here is what I had in my not-so-well attempt to consolidate:

$Consults = $UserTot | where {($.‘User Class’ -eq “Consultant” -or $.‘User Class’ -eq “Contractor”}

$StartDate = Get-Date 03/01/2020
$totincrements = [int][Math]::Ceiling($Consults.count / 15)

for ($j=0; $j -le $totincrements; $j++) {
$multiplier = $j*15
if ($j -eq 0) {
$smallbatch = $Consults.samAccountName | Select -First 15
}
else {
$smallbatch = $Consults.samAccountName | Select -Skip $multiplier
}
Write-Host “The multiplier is $($multiplier)” -ForegroundColor Yellow
Write-Host “The $($j) batch`n`nHas the following amount:$($smallbatch.count)”
#$smallbatch | %{Set-ADUser -AccountExpirationDate $StartDate.AddDays($j) -Credential $credINT}
}

In the first run, it will set the first 15 consultants to have an expiration date of March 1st. On the second run, it will set all but the first 15 in $Consult to March 2, 2020 (total of 1500-15=1485 users). Then on the third run, it will leave users 16-30 at March 2nd but set the rest of the 1470 users to March 3rd and so on. I think it will do the trick, but not really “tight” nor clean.

Ah, I did not know you could combine both -First and -Skip switches within the same select statement. So, my final code should work:

for ($j=0; $j -le $totincrements; $j++) {
$multiplier = $j*15
if ($j -eq 0) {
$smallbatch = $Consults.samAccountName | Select -First 15
}
else {
$smallbatch = $Consults.samAccountName | Select -First 15 -Skip $multiplier
}
Write-Host “The multiplier is $($multiplier)” -ForegroundColor Yellow
Write-Host “The $($j) batch`n`nHas the following users:$($smallbatch)”
#$smallbatch | %{Set-ADUser -AccountExpirationDate $StartDate.AddDays($j) -Credential $credINT}
}

 

Thank you, very much!