Powershell Foreach loop, Powershell For loop explained with example (2021)

Looping in Powershell is a core of the script and we have to make sure to do it correctly. Let’s find out how we can use the looping concept in Powershell with easy examples. 

What is Windows Powershell?

Windows PowerShell is one of the free scripting applications preinstalled in Windows machine or directly can be downloaded from Windows official website. From windows 7 onwards Microsoft introduced Powershell.ise where we can test our scripts and later save them in .ps1 format. Although there are many advantages of using Powershell our main use is to automate things so that we can save time and manual work.

Today, Powershell comes with great cmdlets that can be used to run in any specific application-related servers that can be SQL, Sharepoint, etc.  Well, everything can be automated with Powershell. Although sometimes it is highly recommended to do things manually as the world is progressing to achieve automation and cut manual tasks it is really important to cope with moving technologies.

For Loop in Powershell with example

Without any further ado lets see how actually a for loop works. For loop in Powershell or any other language works same. It uses iteration to run, let’s see an example of For loop. 

A For loop is constructed by setting the value of a variable when entering the loop, the condition on which the loop should be terminated, and an action to be performed against that variable each time through the loop.

The below example shows a for loop and what it will do in each iteration. $i++ is used to iterate the For loop unless the i value is less than 7 i.e. the number of elements present in the array color

				
					$colors = @("Red","Orange","Yellow","Green","Blue","Indigo","Violet")

For ($i=0; $i -lt $colors.Length; $i++)

{
    $colors[$i]
    
}
				
			
For loop in Powershell

Foreach Loop in Powershell with example

If you have followed me or see my scripts you can now probably understood what is the use of Foreach loop in Powershell. In Layman term it is used to iterate the number of items in an array similar to For loop but with less complexity.

Let’s take an example where a array consist of test items 1,2,3. This loop works charm ad output is giveni doctorate.

				
					$comp ="test1","test2","test3"
Foreach ($comps in $comp){
$comps
}
				
			
PowershellGuru.com

While and Do-While loops in Powershell with example

While and Do-While are more oftenly used in coding language and it is also used in Powershell as well. Let’s have a sneak and peek how it actually works.

While loop is also same as the other loops which uses iteration and executes the statement inside it. In Do-while loop the statement executes first later the iteration takes place.

While loop with example

				
					$i=0

while($i -le 10) 

{write-host "hi $i"

$i = $i+1}
				
			
While loop in Powershell

Do-While loop with example

				
					$i=1

do{

write-host "$hi $i"

$i=$i+1

}while ($i -le 10)
				
			
do-while loop in Powershell

Conclusion

I hope you understood the working of the loops in Powershell and use it in your scripts. Let’s have a quick overview of what we covered today. First, we understood For loop with an example and after that, we learnt about Foreach. 

We learned about the difference between While and do-while loop with easy examples. In the coming blog, I would be covering some interesting topics so stay tuned. Subscribe to the notifications to never miss any update from us.

Related Posts

Leave a Reply

Please disable your adblocker or whitelist this site!