O'Reilly logo

Learn PowerShell Toolmaking in a Month of Lunches by Don Jones and Jeffrey Hicks

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

3.8. Looping constructs

Looping constructs are designed to execute some action over and over, either a specified number of times or until some condition is met.

3.8.1. Do...While construct

This is a primary looping construct in PowerShell. It’s designed to repeat a block of commands so long as some condition is True or until a condition becomes True. Here’s the basic usage:

Do {
  # commands
} While ($this -eq $that)

In this variation of the construct, the commands within the curly brackets will always execute at least one time, because the While condition isn’t evaluated until after the first execution. You can move the While, in which case the commands will only execute if the condition is True in the first place:

While (Test-Path $path) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required