Problem
You have two arrays and want to combine them into one.
Solution
To combine PowerShell arrays, use the addition operator (+):
PS >$firstArray = "Element 1","Element 2","Element 3","Element 4" PS >$secondArray = 1,2,3,4 PS > PS >$result = $firstArray + $secondArray PS >$result Element 1 Element 2 Element 3 Element 4 1 2 3 4
Discussion
One common reason to combine two arrays is when you want to add data to the end of one of the arrays. For example:
PS >$array = 1,2 PS >$array = $array + 3,4 PS >$array 1 2 3 4
You can write this more clearly as:
PS >$array = 1,2 PS >$array += 3,4 PS >$array 1 2 3 4
When written in the second form, however, you might think that PowerShell simply adds the items to the end of the array while keeping the array itself intact. This is not true, since arrays in PowerShell (like most other languages) stay the same length once you create them. To combine two arrays, PowerShell creates a new array large enough to hold the contents of both arrays and then copies both arrays into the destination array.
If you plan to add and remove data from an array frequently, the System. Collections.ArrayList class provides a more dynamic alternative.