PSMonday #40: January 30, 2017

Topic: Do-While

Notice: This post is a part of the PowerShell Monday series — a group of quick and easy to read mini lessons that briefly cover beginning and intermediate PowerShell topics. As a PowerShell enthusiast, this seemed like a beneficial way to ensure those around me at work were consistently learning new things about Windows PowerShell. At some point, I decided I would share these posts here, as well. Here’s the PowerShell Monday Table of Contents.

Next up on our language construct journey, is part one of the Do loop: the Do-While. Once you’ve read this week’s, and next week’s PSMonday, be sure to read the full about help topic using the below command.

Get-Help -Name about_Do -ShowWindow

Here’s a couple conceptual examples of the Do-While loop. Of the two, you’ll see me use the second example, in order that my code is easier to read.

Do {<statement list>} While (<condition>)

Do {
    <statement list>

} While (<condition>)

For our first example, we’ll start looping based on the value of a variable. We begin by assigning the numeric value of 1 to the $Number variable. This assignment is done outside of the looping construct. Once set, we begin the loop. On the first pass, we indicate that we’re in the loop, as well as the current value of the $Number variable, we then increase the value of the $Number, and then we check if it is less than (-lt) the numeric value of 10. While this condition is true, we’ll continue to loop, incrementing the value in $Number each time, until the value in $Number variable is no longer less than 10.

$Number = 1

Do {
    Write-Output -InputObject "In the Do loop ($Number)."
    $Number++

} While ($Number -lt 10)

In the Do loop (1).
In the Do loop (2).
In the Do loop (3).
In the Do loop (4).
In the Do loop (5).
In the Do loop (6).
In the Do loop (7).
In the Do loop (8).
In the Do loop (9).

Instead of using a numeric value condition, this time, we’ll use a datetime object instead. Take a look at this next example, and then follow the further below description of each task within the example.

"Current Time: $((Get-Date).ToString())"

$Time = (Get-Date).AddSeconds(10)

"Future Time : $($Time.ToString())"

Do {
    Write-Output -InputObject '--> Waiting for time to pass.'
    (Get-Date).ToString()
    Start-Sleep -Seconds 1

} While ((Get-Date) -lt $Time)

"Current Time: $((Get-Date).ToString())"

Current Time: 1/29/2017 1:46:20 PM
Future Time : 1/29/2017 1:46:30 PM
--> Waiting for time to pass.
1/29/2017 1:46:20 PM
--> Waiting for time to pass.
1/29/2017 1:46:21 PM
--> Waiting for time to pass.
1/29/2017 1:46:22 PM
--> Waiting for time to pass.
1/29/2017 1:46:23 PM
--> Waiting for time to pass.
1/29/2017 1:46:24 PM
--> Waiting for time to pass.
1/29/2017 1:46:25 PM
--> Waiting for time to pass.
1/29/2017 1:46:26 PM
--> Waiting for time to pass.
1/29/2017 1:46:27 PM
--> Waiting for time to pass.
1/29/2017 1:46:28 PM
--> Waiting for time to pass.
1/29/2017 1:46:29 PM
Current Time: 1/29/2017 1:46:30 PM

We began the above example by echoing the current date and time. Then we created the $Time variable and made use of the AddSeconds() method to store the date and time 10 seconds into the future. So we know that time, we echoed it to the screen, as well.

Next, we entered the Do-While construct. Our statement list included writing a string to indicate that we we’re waiting for time to pass, writing the current date and time, and then sleeping for one second. Once these commands were completed, we checked the current date and time as a part of the Do-While’s condition. If the current time was still less than (-lt) ten seconds in the future from when we started ($Time), then we looped again. When that condition was eventually met — when the current time was no longer less than that future time — we exited the Do-While statement and echoed the current time.

We’ll cover the Do-Until next week — a language construct variation that you should probably already be able to figure out based on today’s PSMonday. I’ll say it today and next Monday, as well: The Do loops — both Do-While and Do-Until — ensure that we’ll go through the statement list at least one time. This is to say, that the condition is not evaluated until your code has been executed at least once.

Leave a Reply

Your email address will not be published. Required fields are marked *