Loop are basics of a programming or scripting language. It used for execution of tasks repeatedly for specific times or conditions met

Following types of loops are supported in Powershell

powershell for loop

for loop allows to execute command or code multiple times based on the condition.

Syntax

for (initialization; condition; iteration)
{
    # Statement(s) code block to execute.
}

initialization:for loop starts executeion by initialization for first iteration, Executes first time. COndition: condition is calcualted and execute the code inside a for loop if it is true. Executes for every iteration iteration: Iteration execute for every iteration completetion, Example:

for ($i = 5; $i -ge 0; $i--) {
    Write-Host "$i"
}

when for loop starts its execution, first, variable assigned with 5, then Control goes to check condition, if 5 -ge 0, it is true, executes code inside a block. and control checks iteration ($iā€“) which decrement the current value ie. 4 Again repeates the conditon for every iteraton and executes the code block until condition is false.

code block is write-host $i which prints the variable value to console.

Foreach loop

foreach loop used to iterate the elements of iterated objects and executes code

Iterated objects are arrays and hashtable

Syntax:

foreach($variable in iteratedobject){
  # Statements code to be executed
}

iteratedobject is an array, hashtable, or range of values

Here is an example to iterate the array of elements

$names = @("john", "eric", "mark")
foreach ($name in $names) {
    Write-Host "$name"
}

Iterates an array of elements, prints each name to console.

Hashtable iteration key and values:

$employee = @{
    id = 1
    name = "Eric"
    salary = 5000
}

foreach ($key in $employee.Keys) {
    $value = $employee[$key]
    Write-Host "$key - $value"
}

Output:

name - Eric
id - 1
salary - 5000

hashtable.Keys returns an keys array, use foreach to iterate each key and retrieve the value using $hashtable[key], prints key and values to console.

While loop

while loop to executes the code multiple times based on condition.

This loop always executes and evalautes condition before execution of code inside it

Syntax:

while( condition){
  # Statements code to be executed
}

Condition is expression, evaluated to True and False.

Example

$i = 5
while($i -ge 0){
	    Write-Host "$i"
		$i--

}

Prints 5 4 3 2 1 0

Do while loop

do while loop to executes the code multiple times based on condition. It is similar to while loop, except code executes first and evaluates the condition.

Code executes for every iteration if condition is True.

Syntax

do {
  # Statements code to be executed
}
while( condition)

Condition is expression, evaluated to True and False. For each iteration, It executes code inside it if condition is True.

Example:

$i = 5
do{
	    Write-Host "$i"
		$i--

}while($i -ge 0)

Prints 5 4 3 2 1 0

Break statement

break used in loops to exit the current iteration from the loop and control goes to the next code line of the current block.

$i = 5
do{
	  Write-Host "$i"
		if($i -eq 2){
			break;
		}
		$i--
}while($i -ge 0)
# prints 5432

Above example, prints 5 4 3 2, and breaks the loop

continue statement

continue used in loops to continue the current iteration from the loop.

$i = 5
while($i -ge 0){

		if($i -eq 2){
			$i--
    Write-Host "$i"
			continue;
		}
		$i--

}
# returns 1