How to Loop Through an Array in PowerShell?

In this tutorial, I will explain how to loop through an array in PowerShell. As a PowerShell user, you’ll often encounter situations where you need to iterate through an array to perform operations on each element. I will show you different methods to loop through array in PowerShell.

To loop through an array in PowerShell, you can use the foreach loop. The foreach loop allows you to iterate over each element in the array and perform actions on each element. Here’s an example:

$states = "California", "Texas", "Florida", "New York"

foreach ($state in $states) {
    Write-Host "Processing state: $state"
    # Perform actions on each state
}

In this example, we have an array of state names stored in the $states variable. The foreach loop iterates over each state name in the array, and for each iteration, it executes the code block inside the loop.

PowerShell Loop Through Array

Before checking different methods to loop through an array, let me explain about what is an array in PowerShell.

An array is a collection of elements, such as strings, numbers, or objects, stored in a single variable. In PowerShell, you can create an array by enclosing the elements in parentheses and separating them with commas. For example:

$cities = "New York", "Los Angeles", "Chicago", "Houston"

Now, let me explain different methods that you can use to loop through an array in PowerShell.

Check out Read CSV into Array in PowerShell

1. Loop Through a PowerShell Array with ForEach Loop

The best way to loop through an array in PowerShell is by using the ForEach loop. The ForEach loop iterates over each element in the array and allows you to perform operations on each element individually. Here’s an example:

$servers = "Server01", "Server02", "Server03"

foreach ($server in $servers) {
    Write-Host "Connecting to $server..."
    # Perform operations on each server
}

In this example, we have an array of server names stored in the $servers variable. The ForEach loop iterates over each server name in the array, and for each iteration, it executes the code block inside the loop. In this case, it simply writes a message indicating that it’s connecting to each server.

Here is the exact output you can see in the screenshot below:

powershell loop through array

You can also use the ForEach loop to process file paths or perform actions on specific drives. For instance:

$drivePaths = "C:\Data", "D:\Backups", "E:\Archives"

foreach ($path in $drivePaths) {
    Write-Host "Checking drive path: $path"
    # Perform file operations on each drive path
}

Check out Remove the First and Last Item in an Array in PowerShell

2. Use the Pipeline with ForEach-Object Cmdlet

Another powerful technique for looping through an array in PowerShell is by using the pipeline with the ForEach-Object cmdlet. The pipeline allows you to pass the array elements to the ForEach-Object cmdlet, which then executes a script block for each element. Here’s an example:

$users = "John", "Emma", "Michael", "Olivia"

$users | ForEach-Object {
    Write-Host "Processing user: $_"
    # Perform actions for each user
}

In this example, the $users array is piped to the ForEach-Object cmdlet using the | character. The ForEach-Object cmdlet then executes the script block for each user in the array. The $_ variable represents the current element being processed in each iteration.

Read Count the Number of Objects in an Array in PowerShell

3. Loop Through an Array with For Loop in PowerShell

In addition to the ForEach loop and ForEach-Object cmdlet, you can also use the traditional For loop to iterate through an array in PowerShell. The For loop is useful when you need to access the array elements by their index. Here’s an example:

$states = "California", "Texas", "Florida", "New York"

for ($i = 0; $i -lt $states.Length; $i++) {
    Write-Host "State at index $i is $($states[$i])"
}

In this example, we use the For loop to iterate through the $states array. The loop starts with the index $i set to 0 and continues as long as $i is less than the length of the array. Inside the loop, we access each state using the index $i and display a message with the state name.

Here is the exact output in the screenshot below:

powershell loop through array of strings

Read Split an Array into Smaller Arrays in PowerShell

PowerShell Loop Through Array of Strings

To loop through an array of strings in PowerShell, you can use the foreach loop. The foreach loop allows you to iterate over each element in the array and perform actions on each string. Here’s an example:

$cities = "New York", "Los Angeles", "Chicago", "Houston"

foreach ($city in $cities) {
    Write-Host "Processing city: $city"
    # Perform actions on each city string
}

In this example, we have an array of city names stored in the $cities variable. The foreach loop iterates over each city name in the array, and for each iteration, it executes the code block inside the loop. In this case, it simply writes a message indicating that it’s processing each city.

Here is the exact output in the screenshot below:

powershell foreach array of strings

The foreach loop follows this syntax:

foreach ($element in $array) {
    # Code to execute for each element
}

Here, $element is a variable that represents the current element being processed in each iteration, and $array is the array you want to loop through.

You can perform various actions on each string element inside the loop. For example, you can modify the strings, perform string manipulations, or use the strings in other operations.

Here’s an example that demonstrates converting each city name to uppercase:

$cities = "New York", "Los Angeles", "Chicago", "Houston"

foreach ($city in $cities) {
    $uppercaseCity = $city.ToUpper()
    Write-Host "Uppercase city name: $uppercaseCity"
}

In this example, inside the loop, we use the ToUpper() method to convert each city name to uppercase and store it in the $uppercaseCity variable. Then, we write a message displaying the uppercase city name.

The foreach loop provides a simple and intuitive way to iterate through an array of strings in PowerShell, allowing you to process each string element individually.

Check out How to Loop Through an Array of Objects in PowerShell?

PowerShell foreach array of strings

In PowerShell, you can use the foreach loop to iterate over an array of strings and perform actions on each string element. Here’s an example:

$fruits = "apple", "banana", "orange", "grape"

foreach ($fruit in $fruits) {
    Write-Host "I like $fruit!"
}

In this example, we have an array of fruit names stored in the $fruits variable. The foreach loop is used to iterate over each fruit name in the array.

The foreach loop follows this syntax:

foreach ($element in $array) {
    # Code to execute for each element
}

Here, $element is a variable that represents the current element being processed in each iteration, and $array is the array you want to loop through.

In the example above, the foreach loop iterates over each fruit name in the $fruits array. For each iteration, the current fruit name is assigned to the $fruit variable.

Inside the loop, we have a Write-Host command that prints a message for each fruit. The $fruit variable is used within the message to display the current fruit name.

When you run this code, the output will be:

I like apple!
I like banana!
I like orange!
I like grape!

The foreach loop executes the code block inside the loop for each fruit name in the array, allowing you to perform actions on each string element individually.

You can see the exact output in the screenshot below:

powershell foreach string array

Conclusion

In this tutorial, I explained how to loop through arrays in PowerShell using different methods, including the ForEach loop, the ForEach-Object cmdlet with the pipeline, and the For loop. For all the methods, I explained different real examples of the PowerShell loop through an array.

You may also like:

>