0%

0/1 Lessons

Course Introduction

• 5min

0 / 2 lessons complete

Getting Started with Windows PowerShell

• 56min

0 / 8 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

PowerShell Modules

• 58min

0 / 7 lessons complete

Powershell Challenges

• 1hr 55min

0 / 23 lessons complete

Section Overview
Full Access Account Required

Video | 5 min

The Trusted Hosts List Challenge
Full Access Account Required

Text | 5 min

The Trusted Hosts List Answer
Full Access Account Required

Video | 5 min

Starting a Remote Session Challenge
Full Access Account Required

Text | 5 min

Starting a Remote Session Answer
Full Access Account Required

Video | 5 min

Rename a Computer Challenge
Full Access Account Required

Text | 5 min

Rename a Computer Answer
Full Access Account Required

Video | 5 min

Change a Password on a Remote Computer Challenge
Full Access Account Required

Text | 5 min

Change a Password on a Remote Computer Answer
Full Access Account Required

Video | 5 min

How to Copy Files To-From a Remote Machine Challenge
Full Access Account Required

Text | 5 min

How to Copy Files To-From a Remote Machine using VSC Answer
Full Access Account Required

Video | 5 min

How to Export Logs to a CSV File Challenge
Full Access Account Required

Text | 5 min

How to Export Logs to a CSV File Answer
Full Access Account Required

Video | 5 min

How to Create Multiple Folders on the Host Machine using Powershell
Full Access Account Required

Text | 5 min

How to Create Multiple Folders on the Host Machine using Powershell Answer
Full Access Account Required

Video | 5 min

Who Rebooted the Server ID1074 Challenge
Full Access Account Required

Text | 5 min

Who Rebooted the Server
Full Access Account Required

Video | 5 min

How can you get Info on all your Hard Drives Challenge
Full Access Account Required

Text | 5 min

How can you get Info on all your Hard Drives Answer
Full Access Account Required

Video | 5 min

How can I get a list of CPU's and Installed Printers, Last Boot-up Time Challenge
Full Access Account Required

Text | 5 min

How can I get a List of CPU's and Installed Printers, Last Boot-up Time Answer
Full Access Account Required

Video | 5 min

How can I Automate Tasks with Task Scheduler Challenge
Full Access Account Required

Text | 5 min

How can I Automate Tasks with the Task Scheduler Answer
Full Access Account Required

Video | 5 min

Course Conclusion

• 1min

0 / 1 lessons complete

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.

Create note

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.

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