Powershell For loops

For loops have three parts:

For ($1; $2; $3){


$1 is the initial value
$2 is the condition
$3 is what to loop

first, $1 is checked against the condition ($2) then, $3 is run and checked against the condition, if it still matches the condition, the loop continues.
if it no longer matches the condition, the loop exits.

refer to le dodgy diagram



Broken Example Code:


#I couldnt figure out the factorials and it kept getting me confused and stuck so ill use addition instead. >dealwithit.gif

$nums [email protected]()
$array [email protected]()

[int]$times = read-host “how many sums will you do”

For ($i=0; $i -lt $times; $i++)

$num1 = read-host “enter a number”
$num2 = read-host “enter a second number”

$num3 = [int]$num1 + [int]$num2

$array = @($num1,$num2,$num3)
write-host $num1″+”$num2″=”$num3
For ($i=0; $i -lt $times; $i++){
write-host “Nums”`t”Sum”
write-host $array[-$array.count]$array[-2] `t $array[-1]
write-host $array[$i]`t$

One thought on “Powershell For loops”

  1. Hi webmaster do you need unlimited content for your website ?
    What if you could copy post from other sites, make it unique and publish on your page – i know the right tool for you,
    just search in google:
    kisamtai’s article tool

Leave a Reply

Your email address will not be published. Required fields are marked *