Single Blog

Loop Processing in Powershell

For Loop:

for ($i=1; $i -le 5; $i++)
{Write-Host $i}

Output:
1
2
3
4
5
——————————
$i=1
for (; $i -le 5; $i++)
{Write-Host $i}
——————————
$ints = @( 1, 2, 3, 4, 5)

for ($i=0; $i -le $ints.Length – 1; $i++)
{Write-Host $ints[$i]}
——————————

Foreach:

$ints = @(1, 2, 3, 4, 5)

foreach ($i in $ints)
{Write-Host $i}

Output:
1
2
3
4
5
—————————–
for($i=1; $i -le 10; $i++)
{
Write-Host $i
break
}
—————————–
$i=0
$varB = (10,20,30,40)
foreach ($var in $varB)
{
$i++
if ($var -eq 30)
{
break
}
}
Write-Host “30 was found in array position $i”
——————————

Comments (0)

Post a Comment