function Split-array { <# .SYNOPSIS Split an array .PARAMETER inArray A one dimensional array you want to split .EXAMPLE Split-array -inArray @(1,2,3,4,5,6,7,8,9,10) -parts 3 .EXAMPLE Split-array -inArray @(1,2,3,4,5,6,7,8,9,10) -size 3 #> param($inArray,[int]$parts,[int]$size) if ($parts) { $PartSize = [Math]::Ceiling($inArray.count / $parts) } if ($size) { $PartSize = $size $parts = [Math]::Ceiling($inArray.count / $size) } $outArray = @() for ($i=1; $i -le $parts; $i++) { $start = (($i-1)*$PartSize) $end = (($i)*$PartSize) - 1 if ($end -ge $inArray.count) {$end = $inArray.count} $outArray+=,@($inArray[$start..$end]) } return ,$outArray }
http://gallery.technet.microsoft.com/scriptcenter/Split-an-array-into-parts-4357dcc1