How to Add Values to an Array in PowerShell?

Someone recently asked me about adding values to an array in PowerShell in an online webinar. There are several methods for doing so. In this tutorial, I will explain how to add values to an array in PowerShell with practical examples.

To add values to an array in PowerShell using the += operator, simply append the new item to the existing array. This operator creates a new array that includes the original items plus the new one. For example, if you have an array $names = @(“John”, “Jane”, “Michael”) and you want to add “Emily,” you can use $names += “Emily”. After executing this, the $names array will contain (“John”, “Jane”, “Michael”, “Emily”).

Add Values to an Array in PowerShell

An array is a data structure that holds a collection of items. In PowerShell, you can create an array by enclosing the items in @() parentheses. For example:

$names = @("John", "Jane", "Michael")

Now, let me show you how to add values to an array in PowerShell using different methods with examples.

1. Add Values Using the += Operator

One of the simplest ways to add values to an array in PowerShell is by using the += operator. This operator appends a new item to the array, creating a new array with the original items plus the new one.

Syntax

Here is the syntax to add values to an array.

$array += $newItem

Example

Let’s add a new name to our $names array that we declared above:

$names = @("John", "Jane", "Michael")
$names += "Emily"

After executing the above code, the $names array will contain ("John", "Jane", "Michael", "Emily").

Here is the exact output in the screenshot below:

Add Values to an Array in PowerShell

Check out PowerShell Array Range

2. Using the ArrayList Class

While the += operator is convenient, it can be inefficient for large PowerShell arrays because it creates a new array each time. A more efficient method is to use the ArrayList class from the .NET framework, which allows dynamic resizing.

Syntax

Here is the syntax:

$arrayList = [System.Collections.ArrayList]::new()
$arrayList.Add($newItem)

Example

Let me give you an example.

Here’s how you can add values to an ArrayList in PowerShell:

$arrayList = [System.Collections.ArrayList]::new()
$arrayList.Add("John")
$arrayList.Add("Jane")
$arrayList.Add("Michael")
$arrayList.Add("Emily")

3. Using the Insert Method

If you need to add a value at a specific position in the PowerShell array, you can use the Insert method of the ArrayList class.

Syntax

Here is the syntax:

$arrayList.Insert($index, $newItem)

Example

Now, let me show you an example.

To insert “David” at the second position (index 1) in the PowerShell array:

$arrayList.Insert(1, "David")

Now, the array will be ("John", "David", "Jane", "Michael", "Emily").

Check out PowerShell If Array Contains

4. Combine Two Arrays

Another common scenario is combining two arrays into one. PowerShell makes this easy with the + operator.

Syntax

Here is the syntax:

$combinedArray = $array1 + $array2

Example

Now, let me show you an example.

Let’s combine two arrays of city names in PowerShell:

$cities1 = @("New York", "Los Angeles")
$cities2 = @("Chicago", "Houston")
$allCities = $cities1 + $cities2

After running this, $allCities will contain ("New York", "Los Angeles", "Chicago", "Houston").

This is another indirect way of adding values to a PowerShell array. Here is the exact output in the screenshot below:

powershell add values to array

Check out Associative Arrays in PowerShell

Add Values to an Array in PowerShell Using ForEach

We saw how to add values to a PowerShell array, but sometime you may need to add values dynamically within a loop. Let me explain two methods to add values to an array using a ForEach loop with some examples.

Method 1: Using the += Operator

The += operator is the best method for appending values to an array within a ForEach loop in PowerShell. However, it might be less efficient for large datasets.

Syntax

Here is the syntax:

$array += $item

Example

Now, let me show you an example.

Suppose we have a list of states, and we want to add them to an array:

$states = @("California", "Texas", "Florida")
$stateArray = @()

$states | ForEach-Object {
    $stateArray += $_
}

$stateArray

After running this code, $stateArray will contain ("California", "Texas", "Florida").

I executed the above PowerShell script, and you can see the exact output in the screenshot below:

powershell add values to array foreach

Check out Add Only Unique Values to an Array in PowerShell

Method 2: Using the ArrayList Class

For better performance, especially with large PowerShell arrays, use the ArrayList class from the .NET framework. This method allows dynamic resizing without creating a new array each time.

Syntax

Here is the syntax:

$arrayList.Add($item)

Example

Now, let me show you an example.

Let’s add a list of cities to an ArrayList in PowerShell.

$cities = @("New York", "Chicago", "Houston")
$cityList = [System.Collections.ArrayList]::new()

$cities | ForEach-Object {
    $cityList.Add($_)
}

$cityList

After executing this, $cityList will contain ("New York", "Chicago", "Houston").

By using either the += operator or the ArrayList class within a ForEach loop, you can efficiently add values to an array in PowerShell.

Conclusion

In this tutorial, I explained how to add values to an array in PowerShell using different methods. I always recommend using the += operator for small arrays and the ArrayList class for large arrays. However, if you want to add values to specific positions, you can use the insert method.

Finally, I have also explained how to add values to an array in PowerShell in the foreach loop.

Leave a comment below if these examples help you.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.