Screen Shot 2022-07-24 at 11.52.33 AMScreen Shot 2022-07-24 at 11.52.33 AMScreen Shot 2022-07-24 at 11.52.33 AMScreen Shot 2022-07-24 at 11.52.33 AM
  • Contact
  • Series
    • PowerShell Q&A
  • Blog
Contact NOW
✕

PowerShell Loops Part 2: While, and Do-Until

  • Home
  • Blog
  • Getting Started With PowerShell
  • PowerShell Loops Part 2: While, and Do-Until
Mastering PowerShell Loops: Part 1 – For Loops
01/23/2023
PowerShell Arrays: Storing and Manipulating Data
02/06/2023
Published by iNet on 01/30/2023
Categories
  • Getting Started With PowerShell
Tags
  • Code Execution
  • Do-Until Loop
  • loops
  • PowerShell
  • Programming Language
  • While Loop
PowerShell Loops: While, and Do-Until

In this blog post, “PowerShell Loops: While, and Do-Until”, we will delve into the world of loops and how they can be utilized in the powerful programming language of PowerShell. Loops are a fundamental aspect of any programming language and are used to execute a block of code repeatedly until a certain condition is met. In this tutorial, we will explore two specific types of loops: the while loop and the do-until loop. These two looping constructs function in slightly different ways, but both allow you to repeatedly execute a block of code as needed. We will cover the syntax and usage of each loop, as well as provide examples to help you understand how they can be implemented in your own scripts. So join me as we explore the exciting world of loops in PowerShell!

The While Loop

The while loop is used to repeat a block of code as long as a certain condition is true. Here’s the basic syntax for a while loop in PowerShell:

while (condition) {
    # code to be executed
}

The condition inside the parentheses is evaluated before each iteration of the loop. If it evaluates to $true, the loop will run again. If it evaluates to $false, the loop will exit and execution will continue with the next line of code after the loop.

Here’s an example of a while loop that counts from 1 to 10:

$i = 1
while ($i -le 10) {
    Write-Host $i
    $i++
}

This loop will output the numbers 1 through 10, with each number on a new line. The $i++ statement increments the value of $i by 1 each time the loop runs. Without this, the loop would run indefinitely.

The Do-Until Loop

The do-until loop is similar to the while loop, but it works in the opposite way. Instead of running as long as a condition is true, it runs until a condition becomes true. Here’s the basic syntax for a do-until loop in PowerShell:

do {
    # code to be executed
} until (condition)

The do-until loop will execute the code block at least once, because the condition is not checked until after the first iteration. This means that the loop may run more than once, depending on the condition.

Here’s an example of a do-until loop that counts down from 10 to 1:

$i = 10
do {
    Write-Host $i
    $i--
} until ($i -eq 0)

This loop will output the numbers 10 through 1, with each number on a new line. The $i-- statement decrements the value of $i by 1 each time the loop runs.

Conclusion

In this tutorial, we’ve covered the basics of the while and do-until loops in PowerShell. These loops are useful for executing a block of code repeatedly, based on a certain condition. Whether you choose to use a while loop or a do-until loop will depend on your specific needs and the logic of your code.

I hope this tutorial has been helpful in your journey to learn PowerShell! It was a pleasure for me to provide this information to you.

If this was interesting, check out my other posts;
Randomly Generate a Passphrase Using PowerShell

Share
iNet
iNet

Related posts

03/13/2023

Automate Cursor Movement on Windows with PowerShell


Read more
powershell-icon
02/27/2023

PowerShell Modules: Extending the Capabilities of Your Scripts


Read more
02/20/2023

PowerShell Functions: Organizing Your Scripts and Reusing Code


Read more

Comments are closed.

© Automate and Deploy · Made with | ChenzoDesigns.com
    Contact NOW