The following are PowerShell Loops
For Loop
The for statement or while loop is language construct to create a loop that runs command in a command block as long as the conditional test evaluated to true. For loop is used to iterate an array of values and to operate on a subset of these values. In most cases, if you want to iterate all the values in an array, consider to use Foreach loop. This is loop basic syntax.
Actually you can do this too in for loop.
But when you forget to specify the condition the loop will looping forever. Press CTRL+C to stop this.
for (<init>; <condition>; <repeat>) {
<statement(s)>
}
For loop example.
for ($i = 0; $i -lt 5; $i++) {
Write-Host "Hello number $($i + 1)"
}
for ($i = 1; $i -le 5; $i++) {
Write-Host -n "$i"
}
Write-Host ""
Actually you can do this too in for loop.
$i = 1
for (; $i -le 5;) {
Write-Host "hello"
$i++
}
But when you forget to specify the condition the loop will looping forever. Press CTRL+C to stop this.
While Loop
The while statement or while loop is language construct to create a loop that runs command in a command block as long as the conditional test evaluated to true. It's much simpler, flexible, and easier because the syntax is less complicated than For and Foreach statement because you specify a conditional test that how many times the loop run.
while (condition) {
<statement(s)>
}
For example to display number 1-5.
$i = 1
while ($i -le 5) {
Write-Host -n "$i "
$i++
}
Write-Host "" # for new line feed or "" instead
# or you can do this too
$i = 1
while ($i -le 5) { Write-Host -n "$i " ; $i++ } ""
Notice that -n option is for print each number in one line, and in the second example the semicolon is for separating commands in one line. If you forget to put $i++ then the script will loop forever. You can do CTRL+C to stop looping forever. More example.
$thePassword = "mypass"
$ans = $null
while ($ans -ne "N") {
$pass = Read-Host "Enter the password"
if ($pass -eq $thePassword) {
Write-Host "Login Successed!"
$ans = 'N'
} else {
$ans = Read-Host "Invalid password!. Try Again? (Y/N)"
}
}
ForEach Loop
The Foreach statement or Foreach loop is a language construct for iterating a series of values in a collection of items such as an array. The following are ForEach syntax:
foreach ($<item> in $<collection>) {
<statement(s)>
}
Example
$users = @("Henry", "Nicky", "Laila", "Bob")
foreach ($user in $users) {
Write-Host $user
}
$path = Get-ChildItem "C:\Windows\System32\"
foreach ($item in $path) {
if ($item -like "*.dll") {
Write-Host $item
}
}
You can also do this too.
foreach ($file in Get-ChildItem *.ps1) {
Write-Host $file.Name
}
Do Loop
PowerShell has two Do loops, there are Do-While and Do-Untiil loop, both loops's condition is evaluated after the script block has run that means run at least once before the condition is evaluated. So the script block will keep reapeated as long as the condition evaluates to true. Basic syntax Do-While and Do-Until.
<statement(s)>
} while ( <condition> )
do {
<statement(s)>
} Until ( <condition>)
Example for Do-While and Do-Until.
$i = 1
do {
Write-Host "Hello number $i"
$i++
} while ($i -le 5)
""
$i = 1
do {
Write-Host "Hello number $i"
$i++
} Until ($i -gt 5)
They display same output. Notice the different both conditions. More example in Do-While.
$thePassword = "mypass"
$ans = $null
do {
$pass = Read-Host "Enter the password"
if ($pass -eq $thePassword) {
Write-Host "Login Successed!"
$ans = "N"
} else {
$ans = Read-Host "Invalid password!. Try Again? (Y/N)"
}
} While ($ans -ne "N")
You can also do it in Do-Until
$thePassword = "mypass"
$ans = $null
do {
$pass = Read-Host "Enter the password"
if ($pass -eq $thePassword) {
Write-Host "Login Successed!"
$ans = "N"
} else {
$ans = Read-Host "Invalid password!. Try Again? (Y/N)"
}
} until ($ans -eq "N")
Leave comment if you guys have any feedback and questions.
0 Response to "PowerShell Loops"
Post a Comment