Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using range operator with a step

The PowerShell range operator generates a list of values:

>1..6

1
2
3
4
5
6

How can I generate a list of values with a specific step? For example, I need a list from 1 to 10 with step 2.

like image 850
constructor Avatar asked Sep 13 '25 15:09

constructor


1 Answers

The range operator itself doesn't support skipping/stepping, but you could use Where-Object (or the Where() method if you're running version 4.0 or above) to filter out every second:

PS C:\> (1..10).Where({$_ % 2 -eq 0})
2
4
6
8
10

Version 2.0 and up:

PS C:\> 1..10 |Where-Object {$_ % 2 -eq 0}
2
4
6
8
10
like image 153
Mathias R. Jessen Avatar answered Sep 16 '25 08:09

Mathias R. Jessen