In this PowerShell tutorial I will detail the For loop, ForEach-Object loop, and the Do loops (Do-While and Do-Until).
These loops are lited below for quick reference (I use the standad for loop so infrequantly I have to normaly look it up) then I will go in to more detail on each one.
# A bog standard for loop.
For ($x=0; $x -le 10; $x++)
{
"loop number $x"
}
# A foreach loop
foreach ($Obj in $ObjArray)
{
"This is loop number" + $Obj
}
# A Do - While Loop
Do {
$SomeThing
}
While ($SomeThing -eq $true)
# A Do - Until Loop
Do {
$SomeThing
}
Until ($SomeThing -eq $true)
# While Loop
While ($SomeThing -eq $true) {
$SomeThing
}
1, The standard For loop.
The first part of the loop syntax is the start value, so in this case $x will have an initial value of 0.
The next part is the logic. ie run the loop code if this statment is true. In this case the loop will keep looping while $x is less or equiel to 10.
The last stament is the step. so each time we loop we increace (or decrease) $x by this value.
Examples
For ($x = 1; $x -ge -100; $x = $x – 10) {“Decrease $x by 10 until we get to -100”}
For ($x = 0; $x -le 100; $x ++) {“increase $x by 1 until we get to 100”}
For ($x = 30; $x -ge 100; $x = $x + 2) {“Increase $x by 2 until we get to 100”}
2, The For each loop.
This is the loop I use the most.
Loop through each row of an array. Set the variable $Obj to the row of the array Im looping through.
Examples
foreach ($Line in $ImportedTextFile){write-host $Line}
foreach ($ServiceObj in get-service){ $ServiceObj | select name, status}
3, The Do While loop.
In the Do – While loop we loop through a piece of code while a statment is true.
Note that the evaluation is performed at the end. This means the loop is executed once regardless if the statment is true or not.
Examples
$x = 1
do{
$x
}while($x -le 10) # will keep looping while $x is less then 11.
Note: this will be an infinat loop as the value of $x is not changing
$x = “EverthingIsFine”
do{
do stuff
if stuff fails set $x to “OhMyGodStopTheresAnError!!”
}while($x -ne “OhMyGodStopTheresAnError!!” )
4, The Do Until loop.
Prety much the same and the Do – While loop exept this stops looping with then conditinal statment is true.
So the above examples can be rewriten as Do – Until loops as follows:
Examples
$x = 1
do{
$x
}until($x -gt 10) # will keep looping until $x is more then 10.
Note: this will be an infinat loop as the value of $x is not changing
$x = “EverthingIsFine”
do{
do stuff
if stuff fails set $x to “OhMyGodStopTheresAnError!!”
}until($x -eq “OhMyGodStopTheresAnError!!” )
5, The While loop (no Do)
This loop is almost the same as the Do – While loop exept the contition is evaluated at the start of the loop.
Note that now evaluation is performed at the start. This means the loop may not ever run if the evaluation is initially false.
Examples
$x = 11
while($x -le 10) {
$x
} # will still keep looping while $x is less then 11.
Note: this will be an infinit loop as the value of $x is not changing, but the loop will never execute in the first place as the While statment will evaluate as false.
$x = “IDontKnowIfEverthingIsFine”
while($x -ne “OhMyGodStopTheresAnError!!” ) {
do stuff
if stuff fails set $x to “OhMyGodStopTheresAnError!!”
}