Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Powershell Loop Through a Collection N objects at a time?

I am wondering how one would tackle looping through a collection of objects, processing the elements of that collection in groups instead of singularly, as is the case in normal Foreach loops. For example, instead of this:

$items = get-vm
foreach ($item in $items) { do something }

I would like to do this:

$items = get-vm
foreach ((5)$item in $items) {do something}

Essentially, this statement intends to say foreach 5 items in items do some work.....

Can anyone show me the proper constructs required to accomplish this?

like image 465
user3014016 Avatar asked Oct 27 '25 06:10

user3014016


1 Answers

I've got this:

 $array = 1..100
 $group = 10
 $i = 0

 do {
     $array[$i..(($i+= $group) - 1)]
     '*****'
     }
      until ($i -ge $array.count -1)
like image 181
mjolinor Avatar answered Oct 29 '25 06:10

mjolinor