How to translate MS-DOS for loop to Powershell

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

How to translate MS-DOS for loop to Powershell

Post by MigrationUser »

07 May 2009 15:49
davep


I apologize for the noob question: How would I translate something like this to powershell?

Code: Select all

for /l %X in (1,1,6) do ...
I see from get-help foreach -examples that I can create an array and then process each item, but how to do it with just signifying "start, step,end" like MS-DOS?

Code: Select all

PS C:\> 1,2,3,4,5,6 | foreach-object -process {$_*2}
2
4
6
8
10
12
----------------------------

#2 07 May 2009 18:07
Simon Sheppard


use the range operator
https://ss64.com/ps/syntax-arrays.html

Code: Select all

PS C:\> 1,2,3,(5..9) | foreach-object -process {$_*2}
----------------------------

#3 07 May 2009 18:42
davep


Ah, yes. Thank you. I also just found the range operator in "get-help array" Thanks again!
debater
Posts: 1
Joined: 2022-Oct-16, 1:58 pm

Re: How to translate MS-DOS for loop to Powershell

Post by debater »

Code: Select all

for($i=1; $i -le 10; $i++){Write-Host $i}
as the example on this very web site :-)

Also:

Code: Select all

for($i=1; $i -le 6; $i++){echo ($i*2)}
Post Reply