PowerShell Add Array to Array

PowerShell add array to array

Using + Operator

Use the + operator to add an array to another array.

First, we used the array operator represented by @() to declare two arrays, $array1 and $array2, containing the values 1,2,3and 4,5,6. After that + operator is used to concatenate the two arrays together into a $newArray.

In PowerShell, when you use the + operator with arrays, it creates a new array that contains all the elements from both arrays. So, $newArray contains all six elements: 1, 2, 3, 4, 5, and 6. Finally, we output the $newArray contents to the console.

Using += Operator

Use the += operator to add an array to another array.

The above code block is similar to the previous one but uses the += operator to add $array2 to the end of $array1. The += operator is a shorthand way of writing $array1 = $array1 + $array2, where the elements of $array2 are added to the end of $array1. Finally, the code displays the contents of $array1 on the PowerShell console.

Using , Operator

Using the Comma Operator Array Type Accelerator (,) to add an array to another array.

This snippet is similar to the previous two codes, but it uses the comma operator (,) to create an array of arrays by putting a comma before each array variable; for example, ,$array1 and ,$array2. This creates two single-element arrays, each containing the original array. Finally, the code uses the + operator to concatenate the two single-element arrays and assigns the resulting array to the $newArray variable. The resulting array will contain two elements, each of which is one of the original arrays.

Using Array Constructor

Use the array constructor to add an array to another array.

Here, we use the array constructor to add an array to another array. First, the $newArray is created using the array constructor. Next, the + operator concatenates the two arrays $array1 and $array2, and the resulting array is enclosed in [array]() to create a new array object. Finally, the resulting array is assigned to the variable $newArray and then printed to the console using the variable name.

Using AddRange() & InsertRange() Methods

Use the AddRange() and InsertRange() methods to add an array to another array.

The above code snippet takes two arrays like previous code snippets; then the $newArray variable is created as an instance of the System.Collections.ArrayList class, which provides the AddRange() and InsertRange() methods to add elements to the array.

Please note that final object will be ArrayList in this method.

The AddRange() is used to add the elements of $array1 to $newArray, and the InsertRange() is used to insert the elements of $array2 after the elements of $array1. Finally, the contents of the $newArray variable are printed to the console.

So, we learned that arrays could be combined using various PowerShell methods, such as the +, +=, , operators, array constructor, and AddRange() method. These approaches provide flexibility and allow users to choose a solution that fits their requirements and preferences.

That’s all about PowerShell add array to array.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *