PowerShell Hands-On, for loop while loop $array

()

for loop and while loop in $array are often used including when working with PowerShell scripts. In this tutorial you will show most used example.

PowerShell Hands-On, for loop while loop $array

In the following four examples where array values are created in different loops, these are called up again using the ID

an array can look like this:

$array = @("seattle","paris","bangkok","tokio")
Write-Host $array[0,1,2,3]

Output values with For Loop Array:

The length of the array, or the number of stored values, is read out with $array.length. The variable ($i) serves as a counter to count when to exit the loop. A start value is assigned to the counter ($i=0).The start value should increase by 1 each time the loop passes ($i++) until the final value is reached. The final value is the length of the array ($array.length). When checking the final value, there is a condition: as long as $i is less than the number of values ($i -lt $array.length).

PowershellFor loop

for ($i=0; $i -lt $array.length; $i++){
  Write-Host $array[$i] 
}

The For loop: for ($i=0; $i -lt $array.length; $i++)
Start value $i=0; The variable $i starts with a value of 0
is $i smaller (-lt) $i -lt $array.length condition: the For loop is executed as long as this condition is met: as long as the variable $i is less than $array.length, so as long as $i is less than 4. The action at the loop pass: $i++ means to increase the value of the variable $i by 1, with each pass of the loop $i increases by 1: 0 .. 1 .. 2 .. 3 …

while loop

$i=0
while ($i -lt $array.length){
  Write-Host $array[$i] 
  $i++
}

Example with starting value $i defined before the loop ($i=0)
while ($i -lt $array.length)

Within while is the condition for the loop pass, which loop wid does not leave as long as it is fulfilled:
$i -lt $array.length … as long as $i is smaller $array.length
The variable $i is incremented by 1 within the loop: $i++

Endless Loop

while can be used for an infinite loop as follows: with break, the infinite loop can be exited again. The following example goes through the loop until break is executed, this happens when $i is no longer less than 10:

$i=0
while($true) {
  $i++
  write-host $i
  if ($i -ge 10) {break}
}

do loop

$i=0
do{
  Write-Host $array[$i] 
  $i++
} while ($i -lt $array.length)

Foreach

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

foreach ($i in $array) call all values of the array ($array). The variable $i contains the currently read value for each pass.

Operator Variations:
-gt greater than
-igt greater than, case-insensitive
-cgt greater than, case-sensitive
-ge greater than or equal
-ige greater than or equal, case-insensitive
-cge greater than or equal, case-sensitive
-lt less than
-ilt less than, case-insensitive
-clt less than, case-sensitive
-le less than or equal
-ile less than or equal, case-insensitive
-cle less than or equal, case-sensitive

Conclusion

In this tutorial you learn, how to use for loop and while loop in $array they are often used when working in PowerShell scripting.

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

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