You are currently viewing Loops In PowerShell

Loops In PowerShell

Loops In PowerShell

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed conditions in PowerShell.

https://cloudaffaire.com/conditions-in-powershell/

In this blog post, we will discuss loop in PowerShell. Like any other scripting language, PowerShell also supports loop. A loop in a nut shell is a way to repeat a command or statement until a certain condition is met. PowerShell supports for, foreach, while, do-while and do-until loops.

Loops In PowerShell:

For Loop In PowerShell:

The For statement (also known as a For loop) is a language construct you can use to create a loop that runs commands in a command block while a specified condition evaluates to $true. A typical use of the For loop is to iterate an array of values and to operate on a subset of these values. For loop in PowerShell also supports use of multiple operators at once.

Foreach Loop In PowerShell:

The Foreach statement (also known as a Foreach loop) is a language construct for stepping through (iterating) a series of values in a collection of items. The simplest and most typical type of collection to traverse is an array. Within a Foreach loop, it is common to run one or more commands against each item in an array.

While Loop In PowerShell:

The While statement (also known as a While loop) is a language construct for creating a loop that runs commands in a command block as long as a conditional test evaluates to true. The While statement is easier to construct than a For statement because its syntax is less complicated. In addition, it is more flexible than the Foreach statement because you specify a conditional test in the While statement to control how many times the loop runs.

Do While Loop In PowerShell:

The Do keyword works with the While keyword to run the statements in a script block, subject to a condition. Unlike the related While loop, the script block in a Do loop always runs at least once. A Do-While loop is a variety of the While loop. In a Do-While loop, the condition is evaluated after the script block has run. As in a While loop, the script block is repeated as long as the condition evaluates to true.

Do Until Loop In PowerShell:

The Do keyword works with the Until keyword to run the statements in a script block, subject to a condition. Unlike the related While loop, the script block in a Do loop always runs at least once. Like a Do-While loop, a Do-Until loop always runs at least once before the condition is evaluated. However, the script block runs only while the condition is false.

Hope you have enjoyed this article. In the next blog post, we will discuss break, continue, and exit in PowerShell.

To get more details on PowerShell, kindly follow below official documentation

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about?view=powershell-5.1

 

Leave a Reply