How To Add Multiple Values To Array In PowerShell?

Do you need to add multiple values to an array in PowerShell? In this PowerShell tutorial, I will show you how to add multiple values to an array in PowerShell using various methods and examples.

To add multiple values to an array in PowerShell, you can use the += operator to append each value. For instance, $array += ‘value1’, ‘value2’, ‘value3’ adds three values to the array. Alternatively, you can use the .Add() method if the array is an ArrayList or similar collection type that supports dynamic expansion.

Add Multiple Values To Array In PowerShell

Let us say you have declared an empty array in PowerShell like the below:

$myArray = @()

Now, I will show you different methods to add multiple values to this array in PowerShell.

Method 1: Adding Multiple Values Using += Operator

One of the simplest ways to add multiple values to an array in PowerShell is by using the += operator. This operator appends one or more values to the end of the array.

Here is a complete example:

$myArray = @()
$myArray += 'Value1'
$myArray += 'Value2', 'Value3'

After executing the above commands, $myArray will contain three values: ‘Value1’, ‘Value2’, and ‘Value3’.

You can see the output in the screenshot below:

Add Multiple Values To Array In PowerShell

Method 2: Adding Multiple Values Using .Add() Method

For collections like the ArrayList, you can use the .Add() method to append new values. This method is particularly useful when you are dealing with a collection that will be modified frequently, as it is designed to be more efficient than using the += operator.

Here is a complete example:

[collections.arraylist]$myArray = @()
$myArray.Add('Value1') > $null
$myArray.Add('Value2') > $null

The > $null part is used to suppress the output that .Add() method returns (the index at which the value was added).

Method 3: Adding Multiple Values Using a Subexpression

A subexpression $() allows you to evaluate a statement or a series of statements and return the result. You can use this inside an array declaration to add multiple values at once.

Here is a complete example of how to add multiple values to an array in PowerShell using subexpression.

$myArray = @('Value1', $('Value2', 'Value3'))

In this example, ‘Value2’ and ‘Value3’ are added as separate items in the array, resulting in the same three-element array as before.

Method 4: Adding Multiple Values from Another Array

You can also add multiple values from one array to another by using the += operator or the .AddRange() method if you’re working with an ArrayList.

Here is an example with +=:

$myArray = @('Value1')
$additionalValues = @('Value2', 'Value3')
$myArray += $additionalValues

Here is an example with .AddRange():

[collections.arraylist]$myArray = @('Value1')
$additionalValues = @('Value2', 'Value3')
$myArray.AddRange($additionalValues)

Method 5: Using a Loop to Add Multiple Values

Loops are another way to add multiple values to an array in PowerShell, especially when you want to add a series of values that follow a certain pattern or are generated dynamically.

Here is an example with foreach Loop:

$myArray = @()
foreach ($i in 1..3) {
    $myArray += "Value$i"
}

This loop will add ‘Value1’, ‘Value2’, and ‘Value3’ to $myArray.

Method 6: Using the Pipeline

PowerShell’s pipeline can be used to pass output directly into an array.

Here is an example:

$myArray = 'Value1', 'Value2', 'Value3' | ForEach-Object { $_ }

Here, each value ‘Value1’, ‘Value2’, and ‘Value3’ is passed through the pipeline and added to $myArray.

Note:

  • When using the += operator to add elements to an array, PowerShell actually creates a new array with the additional elements each time, which can be inefficient for large arrays or frequent additions.
  • If you anticipate modifying an array often, consider using an ArrayList or another collection type that is designed for frequent updates.
  • Remember that arrays in PowerShell are zero-indexed, which means the first element is at index 0.

Conclusion

You can use various methods like the += operator, the .Add() method, subexpressions, loops, or the pipeline, etc. to add multiple values to an array in PowerShell. In this PowerShell tutorial, I have explained in detail, how to add multiple values to an array in PowerShell.

You may also like: