PowershellGuru
  • Home
  • Active Directory Scripts
  • Script Repository
  • DHCP Scripts
  • DNS Scripts
  • Blogs
  • Community
  • Login
No Result
View All Result
PowershellGuru
  • Home
  • Active Directory Scripts
  • Script Repository
  • DHCP Scripts
  • DNS Scripts
  • Blogs
  • Community
  • Login
No Result
View All Result
PowershellGuru
No Result
View All Result
Home Powershell Fundamentals

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

PowershellGuru by PowershellGuru
April 25, 2021
in Powershell Fundamentals
0
powershell for loop, powershell foreach
Share on FacebookShare on Twitter

You might also like

Powershell filter

Powershell filter: Using where-object and select-object (2021)

September 9, 2021
Function in PowerShell

A step-by-step guide for function in Powershell in 2021

September 1, 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. 

Powershell For Loop, Powershell Foreach loop

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.

close

DON’T MISS A POST

Keep up to date with PowershellGuru

Powershell Blogs

PowershellGuru provides the best PowerShell scripts available that can be used and download freely. Do Check our blogs to get updated regularly.

Check your inbox or spam folder to confirm your subscription.

PowershellGuru

PowershellGuru

Related Stories

Powershell filter

Powershell filter: Using where-object and select-object (2021)

by [email protected]
September 9, 2021
0

Step-by-step guide for function in PowerShell. Function in Powershell. Function with the return value. Function scope. Function examples.

Function in PowerShell

A step-by-step guide for function in Powershell in 2021

by [email protected]
September 1, 2021
0

Step-by-step guide for function in PowerShell. Function in Powershell. Function with the return value. Function scope. Function examples.

error handling in powershell

Understanding Error handling in Powershell [2021]

by [email protected]
August 20, 2021
0

Error handling in Powershell. Error handling in Powershell . Error handling in Powershell. PowershellGuru. Try block. Throw block. Catch block....

Useful Powershell commands

10 Useful Powershell commands that everyone should know

by [email protected]
August 10, 2021
0

Powershell cmdlets. Powershell cmdlets list. Powershell cmdlets example. Get-process. Get-command. Set-executionpolicy. Clear-history. Stop-process.

Next Post

Understanding Conditional logic in Powershell (2021)

Please login to join discussion

Recommended

PowerShell tips and tricks

5 Useful PowerShell Tips and Tricks

May 15, 2022
Useful Powershell commands

10 Useful Powershell commands that everyone should know

August 10, 2021

About

Dhrub Bharali

PowerShell Enthusiast

Dhrub is hardcore Powershell enthusiast, he has wriiten more than 100 powershell scripts and he is the sole owner of PowerShellGuru.

Follow Us

Popular Story

  • Installing software remotely using powershell

    Easy way to install software remotely using PowerShell (2021)

    827 shares
    Share 331 Tweet 207
  • Detect Log4j vulnerable servers using PowerShell

    753 shares
    Share 301 Tweet 188
  • How to find NTP Server using PowerShell?

    738 shares
    Share 295 Tweet 185
  • Get-LocalGroupMember: Find Local admin using PowerShell (2021)

    728 shares
    Share 291 Tweet 182
  • Get installed software list quickly using PowerShell (2021)

    695 shares
    Share 278 Tweet 174
  • Home
  • Active Directory Scripts
  • Script Repository
  • DHCP Scripts
  • DNS Scripts
  • Blogs
  • Community
  • Login

© 2022 PowershellGuru- PowerShell Scripts For Automation