The Short Answer

If you want to join arrays in powershell, especially if you don't know if one or more of the objects you are joining are arrays or single objects, combine them with the addition '+' operator and cast the first item as an array.

$c = [array]$a + $b

The Long Answer

The most common way to combine two arrays is to use the with the addition '+' operator.

$a = 1,2
$b = 3,4
$c = $a + $b
$c
1
2
3
4

But sometimes you don't know if a variable is an array or a single item. Sometimes cmdlets will return an array if there are more than one result, but if there is only one result then they will return a single item; not an array that contains one item.

If you attempt to combine a single item and an array, then you can run into trouble

$a = 1
$b = 2,3
$c = $a + $b
Method invocation failed because [System.Object[]] does not 
contain a method named 'op_Addition'.
At line:1 char:1
+ $c = $a + 
$b
+ ~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: 
    (op_Addition:String) [], RuntimeException
        + FullyQualifiedErrorId : 
        MethodNotFound

It errors out because the value of $a, which is in this case in integer, doesn't support addition with an array.

If you reverse $a and $b it will work, because $b, an array, supports the addition operator with any type of object; it adds the object to the array.

$c = $b + $a
$c
2
3
1

So to join variables that may, or may not, be arrays. There are a few methods.

This is the most conventional way.

$c = @()
$c += $a
$c += $b
$c
1
2
3

That method works but takes one line plus a line for each item or array. It can be done in a single line, but one of the methods isn't very friendly. I'll show it first, and illustrate the issues with it.

There is a unary array operator, the comma, that creates a single object array.

$c = ,1
$c.GetType()
IsPublic IsSerial Name                                     BaseType                                                    

-------- -------- ----                                     --------                                                    

True     True     Object[]                                 System.Array 

You can see that the comma turns the single integer into an array. So lets say you know that $a isn't an array, you can do this.

$c = ,$a + $b
$c
1
2
3

That works as expected, but what if you don't know if $a is an array or not? Well, when the unary array operator is used on an array, it makes a nested array; an array of a single item, that item being the original array.

$a = 1,2,3
$b = ,$a
$b
1
2
3
$b.count # This shows that $b is an array with one item</code></pre>
1

Simply outputting $b looks normal, but you will see that $b has a count of 1. One would think that it would have a count of 3.

The original array is hidden in the first item of the new array.

$b[0]
1
2
3
$b[0].count
3

So the unary array operator is only good if you know that the first variable is an array or not.

The next method works whether you know the variables are arrays or not.

With this method we will use type casting to cast the first variable as an array. Type cast an array by putting [array] before the variable.

$a = 1
$b = 2,3
$c = [array]$a + $b
$c
1
2
3
$c.count
3
$c = [array]$b + $a
$c
2
3
1
$c.count
3

Since powershell is going to process the combining from left to right, and having single items on the right of the addition operator is ok, you only have to cast the first variable in the operation.

$a = 1
$b = 2,3
$c = 4
$d = 5,6,7
$e = [array]$a + $b + $c + $d
$e
1
2
3
4
5
6
7
$e.count
7

Even though $c is only a single integer, and $d is an array, it works.

Next Post Previous Post