PowerShell – Looping

There may be a situation when you need to execute a block of law several number of times. In generalstatements are executed succession ally The first statement in a function is executed firstfollowed by the alternate, and so on.

Programming languages give colorful control structures that allow for more complicated prosecution paths.
circle statement allows us to execute a statement or group of statements multiple times and following is the general form of a circle statement in utmost of the programming languages −

 

For loop – Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable.

> $array = @("item1", "item2", "item3")
> for($i = 0; $i -lt $array.length; $i++){ $array[$i] }
item1
item2
item3

ForEach loop- Enhanced for loop. This is mainly used to traverse collection of elements including arrays.
> $array = @("item1", "item2", "item3")
 
> foreach ($element in $array) { $element }
item1
item2
item3
 
> $array | foreach { $_ }
item1
item2
item3

while loop - Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.
> $array = @("item1", "item2", "item3")
$counter = 0;

while($counter -lt $array.length){
   $array[$counter]
   $counter += 1
}
 
item1
item2
item3

do.. while loop- Like a while statement, except that it tests the condition at the end of the loop body.
> $array = @("item1", "item2", "item3")
$counter = 0;

do {
   $array[$counter]
   $counter += 1
} while($counter -lt $array.length)
 
item1
item2
item3 

168 thoughts on “PowerShell – Looping”

  1. As I am looking at your writing, baccaratsite I regret being unable to do outdoor activities due to Corona 19, and I miss my old daily life. If you also miss the daily life of those days, would you please visit my site once? My site is a site where I post about photos and daily life when I was free.

    Reply
  2. Can I just say what a relief to search out somebody who truly knows what theyre speaking about on the internet. You undoubtedly know easy methods to deliver a difficulty to mild and make it important. More people need to learn this and perceive this side of the story. I cant believe youre not more standard since you undoubtedly have the gift.

    Reply

Leave a Comment