Instructions
Q&A (0)
Notes (0)
Resources (0)
Saving Progress...
Resources
There are no resources for this lesson.
Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.
In this lecture, I am going to discuss While Loops.
Now, what I am going to do first is open PowerShell ISE as an Administrator.
Let's discuss what a While Loop is. A loop that will repeat a set of commands indefinitely until a condition is met.
It is somewhat similar to the For Loops except for the fact that a For Loop is just counting from 0 to 5, or 0 to 10, or whatever the number may be, and only repeat the set of instructions until a count is done.
A While Loop will run these instructions indefinitely until a condition is met.
If we were to write a For Loop you might remember we write it like this:
for ($i = 0; $i -lt 5; $i++) {
$i
}
If we execute this we get 0 through 4
And, if we want to repeat this in a While Loop we will type:
$i = 0
while ($i -lt 5) {
$I
$I++
}
If we execute this code we get the same result
You might be wondering, why would we ever use a While Loop if we can use a For Loop? Well, if you are going to be counting from 0 to 5 you want to use a For Loop but if you have other dependencies or other conditions there may be cases where you want to use a While Loop.
For example, let’s make a While Loop:
while ((Read-Host “Type ‘xyz’ to stop the loop”) -ne “xyz”) {
echo “You did not type ‘xyz’ so I am going to continue the loop”
}
So if we execute this script it will continue to run indefinitely until I enter “xyz”
Now, there’s also another way you accomplish the same goal and we can do this with a variable. Instead of using Read-Host in the While Loop, we can store that Read-host in a variable and then check that variable against the While Loop.
So let’s type the following using a variable.
while ($userInput -ne “xyz”) {
$userInput = Read-Host “Please type ‘xyz’ to stop the loop”
}
If you execute the code you will see it finishes and is because $userInput is cached. Type the following to confirm:
To clear this issue we need to add to the code the following:
while ($userInput -ne “xyz”) {
$userInput = Read-Host “Please type ‘xyz’ to stop the loop”
}
$userInput = $null
Run it once and you will see it exits one time but it assigns at the end the $null value. Let’s execute it again to enter the loop.
Once running you can enter anything and the loop will continue running indefinitely until I type “xyz” to exit.
Now, let’s create another While Loop and we will base it on the previous While Loop.
while ($userInput -ne “quit”) {
# Gather user input
$userInput = Read-Host “Say ‘yes’ if you like scripting (enter ‘quit’ to stop the loop)”
# Evaluate the user input
If ($userInput -eq “yes”) { echo “I also love scripting! That’s cool” }
}
$userInput = $null
Now, if we execute this script I can type “yes’ and it will continue to execute. If I type “quit” it exits the loop.
And we can build out the script and type more like:
while ($userInput -ne “quit”) {
# Gather user input
$userInput = Read-Host “Say ‘yes’ if you like scripting (enter ‘quit’ to stop the loop)”
# Evaluate the user input
If ($userInput -eq “yes”) { echo “I also love scripting! That’s cool” }
elseif ($userInput -eq “no”) { echo “I really hate scripting too”}
}
$userInput = $null
I can type “yes”, “no”, a blank space to test, and “quit” to exit the script.
So, that’s a good example of how you can use While Loops and why they are different from For Loops.
For Loops and While Loops are the most common loops that you can probably use except for maybe For Each, For Each-Object which we will get into later on in this course.