Table of Contents
TogglePowerShell is a useful tool for everyone whether you’re a programmer or not. It helps automate tasks, both simple and complex which can save businesses a lot of time and effort. However, one challenging part of automation is working with loops. Loops are crucial for automating repetitive and time-consuming tasks.
In this guide, we’ll break down how to use different types of loops in PowerShell such as the For loop, ForEach-Object loop, and the While, Do-While and Do-Until loops.
What is PowerShell?
PowerShell is an open-source scripting language created to help people automate tasks and create useful tools for everyday work. It was designed to be easy to understand, making it ideal even for those who aren’t experienced in programming.
PowerShell serves two main purposes: it acts as a command-line shell, similar to the Windows command prompt (cmd.exe) and as a powerful scripting language that allows you to automate almost any task you need.
Back in 2002, a Microsoft employee named Jeffrey Snover saw that the Windows command-line interface wasn’t as advanced as Linux, a competing system. To address this, he came up with a plan to improve the old command lines which led to the creation of PowerShell.
The program was so successful during its three-year rollout at Windows that, starting with Windows 7 in 2009, PowerShell was included by default on all Windows systems.
Did You Know?
Since 2016, PowerShell has been available on macOS, Linux and other Unix-based operating systems.
Much like other command-line interfaces, PowerShell uses binary commands to perform many tasks. For example, you can use commands to read files, ping other computers or delete registry keys. PowerShell also has special commands called cmdlets—these are pre-built, compiled commands that help users create tools for their scripts.
PowerShell commands follow a verb-noun structure. The verb describes the action and the noun tells what the command affects. For instance, Copy-Item copies a file and Get-Content retrieves text from a file.
If you’re unsure about which verb to use, you can type Get-Verb to see a list of accepted verbs. While there aren’t predefined nouns, you can discover available objects (nouns) by using the Get-Command function.
Tip
If you want to see which verbs are allowed, use the Get-Verb command.
Programming with Loops
In PowerShell, loops are used to repeat a set of commands multiple times. They are perfect for performing tasks repeatedly, whether for a specific amount of time or over a certain number of records. Loops can make your scripts simpler and even help you create interactive menus. PowerShell also has cmdlets especially those starting with “Get,” that return objects with large amounts of similar data.
PowerShell offers several types of loops and often, more than one type can be effective for a task. It’s important to choose the most efficient loop based on either performance or how easy the code is to read.
ForEach-Object loop

In many situations, the ForEach-Object cmdlet is the most efficient way to loop through an object. At its core, ForEach-Object just needs an object to loop through and a script block that contains the commands to be applied to each item in that object.
You can specify these details using the -InputObject and -Process parameter names or you can pipe the object directly to ForEach-Object and use the script block as the first parameter. Below is an example that shows two different ways of using ForEach-Object to loop through the files in a user’s Documents folder.
$myDocuments = Get-ChildItem
$env:USERPROFILE\Documents -File
$myDocuments | ForEach-Object {$_.FullName}
ForEach-Object -InputObject
$myDocuments -Process {$_.FullName}
In some cases, it can be helpful to run certain actions right before or after the loop. You can use the -Begin and -End parameters to set up script blocks that execute before or after the main loop. This is useful if you need to adjust a variable or perform any setup or cleanup tasks before or after the loop runs.
ForEach-Object has two shortcuts—ForEach and %—and starting from PowerShell 3.0, it supports a shorter way to write commands. The three examples below all do the same thing:
Get-WMIObject Win32_LogicalDisk | ForEach-Object {$_.FreeSpace}
Get-WMIObject Win32_LogicalDisk | ForEach {$_.FreeSpace}
Get-WMIObject Win32_LogicalDisk | % {$_.FreeSpace}
Did You Know?
Since loops perform repeated actions on a set of data, you can use PowerShell with a ForEach loop to sync files and folders.
PowerShell For loop

For loops are commonly used to run a set of commands a certain number of times. You can use them to go through an array or object or simply repeat the same code as needed. A For loop is structured by:
- Setting an initial value for a variable when the loop starts.
- Defining the condition for when the loop should stop.
- Specifying an action to update the variable after each loop cycle.
Here’s an example of a basic For loop that creates a multiplication table:
For ($i=0; $i -le 10; $i++) {
“10 * $i = ” + (10 * $i)
}
You can use For loops to go through the values in an array by starting with the first index and increasing the value until you reach the array’s length. You do this by putting the incremented variable inside square brackets right after the array name. Here’s an example:
$colors = @(“Red”, “Orange”, “Yellow”, “Green”, “Blue”, “Indigo”, “Violet”)
For ($i=0; $i -lt $colors.Length; $i++) {
$colors[$i]
}
In this example, the loop will display each color from the array one by one.
While, Do-While and Do-Until loops

The third type of loop PowerShell supports lets you set a condition that controls whether the loop continues running as long as the condition is true or until it is met. While and Do-While loops run as long as the condition is $true and the difference between them lies in their syntax. Do-Until loops work similarly to Do-While loops but stop once the condition becomes true.
Both Do-While and Do-Until loops start with the Do keyword followed by a block of code and are followed by the condition keyword (While or Until) and the condition itself. Here’s an example showing both loops, with the condition reversed:
$i = 1
Do {
$i
$i++
} While ($i -le 10)
$i = 1
Do {
$i
$i++
} Until ($i -gt 10)
While loops and Do-While loops work the same way, but their syntax is slightly different. The While loop only uses the While keyword, followed by the condition and the script block. Here’s an example of a While loop that works just like the previous Do-While example, using the same condition:
$i = 1
While ($i -le 10) {
$i
$i++
}
You can use any of these three loops — Do-While, Do-Until or While — to loop indefinitely. To do this, you set the condition to $true for While and Do-While loops or $false for Do-Until loops.
It’s important to know which type of loop—For, While, Do-While or Do-Until—is best suited for your script.
In certain cases, you might need to stop a loop early for reasons other than the loop’s condition. To do this, you can use the Break keyword to exit the loop. Here’s an example that uses an infinite loop and the Break command to stop it at the right moment:
$i = 1
While ($true) {
$i
$i++
if ($i -gt 10) {
Break
}
}
This loop will run indefinitely but will exit once $i is greater than 10.
Tip
You can make your scripts more organized and easier to manage by wrapping your loops and other code in functions. This way, you can create and use modules in PowerShell.
Why Businesses Should Use PowerShell & Loops
CRM systems and EMR platforms are great at automating repetitive tasks and workflows. However, they can limit you when it comes to customization as you have to work within their preset options.
PowerShell loops—like For, ForEach-Object, While, Do-While and Do-Until—offer much more flexibility. They let you automate tasks like data entry, handle large data sets on a schedule and integrate different systems just the way you need.
By reducing the time you and your team spend on repetitive tasks, you can focus more on strategic actions that help grow your business.
FAQs
What is a Windows PowerShell For loop?
A For loop in PowerShell is used to run a block of code a set number of times. It’s great for performing repetitive tasks by defining a starting point, a condition for when to stop and how to update the loop variable after each cycle.
How do you use a For loop with an array in PowerShell?
You can use a For loop to go through an array by starting at the first index and increasing the index number until you’ve gone through all the elements. Here’s an example:
$array = @(“Apple”, “Banana”, “Cherry”)
For ($i = 0; $i -lt $array.Length; $i++) {
$array[$i]
}
How do you write a PowerShell For loop from 1 to 10?
To create a For loop that runs from 1 to 10, you can use this syntax:
For ($i = 1; $i -le 10; $i++) {
Write-Output $i
}
This will print the numbers 1 through 10.
What is the syntax for a For loop in PowerShell?
The basic syntax for a For loop in PowerShell is:
For (initialization; condition; action) {
# Code to execute
}
Example:
powershell
Copy code
For ($i = 0; $i -lt 5; $i++) {
Write-Output $i }