How's this:
$bigList = ("Name","One","Two","Three","Four","Five","six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen")
$counter = [pscustomobject] @{ Value = 0 }
$groupSize = 5
$bigList[1..$biglist.count] |
Group-Object -Property { [math]::Floor($counter.Value++ / $groupSize) } |
foreach-object {
$_.Group.Insert(0,$biglist[0])
write-output $_
}
Output:
Count Name Group
----- ---- -----
5 0 {Name, One, Two, Three...}
5 1 {Name, six, Seven, Eight...}
3 2 {Name, Eleven, Twelve, Thirteen}
This works by stripping out the first element ("Name") and sending the rest of the array through the group-object cmdlet. The resulting objects are modified in the foreach-object scriptblock by inserting the value from the first element of the big list into the group property. The modified object is re-written to the pipeline.
Assuming you want these in an list of arrays, you'd need to modify the code slightly:
$arrayList = $bigList[1..$biglist.count] |
Group-Object -Property { [math]::Floor($counter.Value++ / $groupSize) } |
ForEach-Object {
$_.Group.Insert(0,$biglist[0])
,$_.group
}
$arraylist |
% {
write-host ( $_ -join "," )
}
Here, we deliberately output only the group property of the groupinfo object that comes from group-object. In addition, we use the , operator to force powershell to output an individual array. If this is not done, we end up with a single array that is just like $biglist, but with extra instances of 'Name' every 5 elements. The final chunk of code outputs the contents of each element of $arrayList, concatenating with commas to demonstrate that we do indeed have an array of arrays:
Name,One,Two,Three,Four,Five
Name,six,Seven,Eight,Nine,Ten
Name,Eleven,Twelve,Thirteen
group-objectmethod. That method assigns a dynamic property to each element and groups accordingly. Each element will end up in one group and one group only. I suspect you'll need to programmatically break up$biglist, stripping off the first element. After that,group-objectmay be used to generate the individual groups, but further processing will be necessary to prefix each group with the first element from the original biglist.