Split a PowerShell Collection into Fixed-Size Batches
PowerShell
function Split-Chunk {
param(
[Parameter(Mandatory)][object[]]$Items,
[ValidateRange(1, 100000)][int]$Size = 100
)
$batches = [System.Collections.Generic.List[object]]::new()
for ($i = 0; $i -lt $Items.Count; $i += $Size) {
$end = [Math]::Min($i + $Size, $Items.Count) - 1
$batches.Add($Items[$i..$end]) # one array per batch
}
return $batches.ToArray()
}