While Loops

Sign up to access this lesson

Click here to sign up and get access to this lesson!

Saving Progress...

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.

Sign up to access the rest of this lesson

You must either log in or sign up to access this lesson.

Saving Progress...

0 0 votes
Lesson Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments

Administration and Automation with Windows PowerShell

0%

0/1 Lessons

Course Introduction

• 5min

0 / 2 lessons complete

Getting Started with Windows PowerShell

• 42min

0 / 7 lessons complete

Getting Help and Finding Commands

• 39min

0 / 6 lessons complete

PowerShell Command Syntax

• 33min

0 / 5 lessons complete

PowerShell Objects and Properties

• 35min

0 / 6 lessons complete

The PowerShell Pipeline

• 24min

0 / 2 lessons complete

PowerShell Providers

• 30min

0 / 5 lessons complete

PowerShell Arrays and Variables

• 28min

0 / 4 lessons complete

PowerShell Loops

• 19min

0 / 3 lessons complete

PowerShell Conditional Statements

• 11min

0 / 1 lessons complete

On Premises Lab Setup

• 36min

0 / 8 lessons complete

Basic Domain Administration with Windows PowerShell

• 2hr 27min

0 / 10 lessons complete

Send Emails with PowerShell

• 22min

0 / 2 lessons complete

PowerShell Desired State Configuration (DSC) Basics

• 1hr 48min

0 / 6 lessons complete

Course Conclusion

• 1min

0 / 1 lessons complete