PowerShell

Loop Statements

Loop statement is a statement that is used to iterate with a set of values to perform or show a sequence of values or computation until a condition is getting False. There are different types of Loop statement

  • for
  • foreach
  • while
  • do .. while

For Loop

It is an iterative statement is used to iterate until the condition stated false. Take a look the following



for($a=1; $a -lt 5; $a++){
    Write-Host $a
}
OUTCOME
1
2
3
4

While Loop

while loop is iterating until the condition is getting false, have a look the following statement which is showing a value

$i=1
while($i -lt 5){
    Write-Host $i
    $i = $i +1
}
OUTCOME
1
2
3
4

ForEach

foreach let to iterate in an array accessing each element in the array. This is a repetitive operation until all the elements in the array is displayed

$array = {'Addis','Lemu','Dire'}
foreach($eachVal in $array){
    Write-Host $eachVal
}
'Addis','Lemu','Dire'

Do While

This is another loop statement that is executing contents in an iterative manner to show or do a computation. The application of a d .. while

$a = 5
do{
    Write-Host $a
    $a = $a -1
}while ($a -gt 0)
OUTCOME
5
4
3
2
1
Scroll to Top