Invoke-BatchArray.ps1
19 Sep 2025Description
Purpose
Takes an array and breaks down into an array of arrays by a supplied batch size
Detailed Description
No detailed description provided.
Usage
Example 1
Invoke-BatchArray -Arr @(1,2,3,4,5,6,7,8,9) -BatchSize 5 | ForEach-Object { Write-Host $_ }
Notes
No additional notes.
Script
function Invoke-BatchArray {
<#
.SYNOPSIS
Takes an array and breaks down into an array of arrays by a supplied batch size
.EXAMPLE
Invoke-BatchArray -Arr @(1,2,3,4,5,6,7,8,9) -BatchSize 5 | ForEach-Object { Write-Host $_ }
#>
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, HelpMessage = "Array to be batched.")]
[object[]]$Arr,
[Parameter(Mandatory = $false, HelpMessage = "Number of objects in each batch.")]
[int]$BatchSize = 5
)
for ($i = 0; $i -lt $Arr.Count; $i += $BatchSize) {
, ($Arr | Select-Object -Skip $i -First $BatchSize)
}
}
Download
Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.
Report Issues
You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.