PowerShell Append to Array [With Examples]

In PowerShell, appending to an array means adding one or more items to the end of the array. In this PowerShell tutorial, I will explain how to append to an array in PowerShell using different methods with examples.

To append an element to an array in PowerShell, you can use the += operator. For example, $array += 'newElement' will add ‘newElement’ to the end of the array $array. This creates a new array with the additional element because PowerShell arrays are fixed in size and cannot be dynamically expanded.

PowerShell Append to Array

An array in PowerShell is a data structure that holds a collection of items. These items can be numbers, strings, objects, or even other arrays. In PowerShell, arrays are incredibly flexible and can even store items of different types.

To create an array in PowerShell, you simply assign multiple values to a variable, separated by commas:

$myArray = 1, 2, 3, 4

This creates an array with four integer elements.

Now, let’s explore how to append items to the above PowerShell array.

Method 1: Using the += Operator

The simplest way to append an item to an array in PowerShell is by using the += operator. This operator takes the existing array and adds another item to the end.

$myArray = 1, 2, 3, 4
$myArray += 5
$myArray

After this operation, $myArray will contain 1, 2, 3, 4, 5.

Here is another example of a string array in PowerShell.

# Define a string array
$myArray = 'Los Angeles', 'Chicago', 'San Antonio', 'San Diego', 'Phoenix'
$myArray += 'Houston'
$myArray

Once you run the above PowerShell script using VS code, you can see the output in the screenshot below.

powershell append to array

Method 2: Using the Array + Array Syntax

You can also append an entire array to another array using the + operator in PowerShell. This is useful when you want to combine two arrays into one.

$firstArray = 1, 2, 3
$secondArray = 4, 5, 6
$combinedArray = $firstArray + $secondArray

Now, $combinedArray will hold 1, 2, 3, 4, 5, 6.

This time, I executed the PowerShell script using PowerShell ISE, and you can see the output in the screenshot below.

Append to an Array in PowerShell

Method 3: Using the ArrayList Class

Another method to append items to an array is by using the ArrayList class from .NET in PowerShell. This class provides a method called Add that you can use to append items.

First, you’ll need to create an instance of the ArrayList:

$arrayList = New-Object System.Collections.ArrayList
$arrayList.AddRange($myArray)

Now, you can append items using the Add method:

$arrayList.Add(5)

The advantage of using ArrayList is that it is designed for dynamic changes, such as adding or removing items. However, the Add method returns the index of the new item, so if you don’t need this information, you can cast it to [void] to suppress the output:

[void]$arrayList.Add(5)

Method 4: Using the += Operator with a Sub-Expression

You can append a complex expression result to an array in PowerShell using the += operator with a sub-expression $():

$myArray += $(if ($someCondition) { "value1" } else { "value2" })

This appends “value1” if $someCondition is $true, otherwise “value2” is appended.

Method 5: Using Add-Range Method of ArrayList

If you have a list of items to append, instead of adding them one by one, you can use AddRange method of an ArrayList.

$newItems = 6, 7, 8
[array]$arrayList.AddRange($newItems)

Note: Appending to arrays in PowerShell using the += operator is simple but not always efficient, especially for large arrays. This is because each time you append an item using +=, PowerShell creates a new array with the additional item, then replaces the old array with the new one. For large arrays, this can be resource-intensive.

PowerShell append to an array of strings

Let us see how to append to an array of strings in PowerShell.

Appending to an array of strings in PowerShell can be done using the += operator. This operator creates a new array with the additional elements because PowerShell arrays are immutable. Below is an example of how to append to an array of strings:

# Define an array of strings
$stringArray = @('apple', 'banana', 'cherry')

# Append a new string to the array
$stringArray += 'date'

# Display the modified array
$stringArray

When you run this script, the output will be an array containing ‘apple’, ‘banana’, ‘cherry’, and ‘date’. Keep in mind that each time you use +=, PowerShell creates a new array with the additional item, which may not be efficient for large arrays or frequent operations.

PowerShell append to array new line

Let us see how to append a new line to an array in PowerShell.

Appending a new line to an array of strings in PowerShell and then outputting each element on a new line can be achieved by using the += operator to add elements and then iterating through the array or converting it to a string with new lines. Here’s a complete example:

# Define an array of strings
$stringArray = @('apple', 'banana', 'cherry')

# Append a new string to the array on a new line
$stringArray += 'date'

# Output each element on a new line
$stringArray -join "`n"

The -join "n”` part of the script will concatenate the array elements into a single string, with each element separated by a newline character. When you run this script, it will output:

apple
banana
cherry
date

Each string in the array appears on a new line in the output.

PowerShell append to array in loop

Sometimes, you might need to append to an array in a loop in PowerShell. Here’s an example where we’re appending a series of numbers to an array using a loop:

# Define an empty array
$numberArray = @()

# Loop through numbers 1 to 5
for ($i = 1; $i -le 5; $i++) {
    # Append each number to the array
    $numberArray += $i
}

# Display the array
$numberArray

In this script, we start by defining an empty array $numberArray. We then use a for loop to iterate over numbers 1 through 5. Inside the loop, we append each number to the array using the += operator. After the loop completes, we output the array containing the numbers 1, 2, 3, 4, and 5.

Remember that this method is not the most efficient for large arrays or a high number of iterations because each time you append an element, PowerShell has to create a new array with the added element.

For more efficient appending, especially in large loops or with large arrays, you might consider using a System.Collections.ArrayList or a System.Collections.Generic.List object, which allows for dynamic resizing of the array.

Conclusion

We can append to an array in PowerShell using different methods like:

  • Using the += Operator
  • Using the Array + Array Syntax
  • Using the ArrayList Class
  • Using the += Operator with a Sub-Expression
  • Using Add-Range Method of ArrayList

For beginners, the += operator is the simplest method to start with. As you advance, you might prefer using ArrayList for better performance with large data sets or when you need more complex operations.

You may also like: