0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Merge Two Arrays Easily

In PowerShell, you can merge two arrays using the syntax $merged = $array1 + $array2, which combines both arrays into one.
📋

Examples

Input[1, 2, 3] and [4, 5, 6]
Output[1, 2, 3, 4, 5, 6]
Input['apple', 'banana'] and ['cherry', 'date']
Output['apple', 'banana', 'cherry', 'date']
Input[] and [10, 20]
Output[10, 20]
🧠

How to Think About It

To merge two arrays, think of putting all items from the first array and then adding all items from the second array right after. In PowerShell, the plus operator + joins arrays by placing elements of the second array after the first.
📐

Algorithm

1
Get the first array input.
2
Get the second array input.
3
Use the plus operator <code>+</code> to combine both arrays.
4
Store the result in a new variable.
5
Return or print the merged array.
💻

Code

powershell
$array1 = 1, 2, 3
$array2 = 4, 5, 6
$merged = $array1 + $array2
Write-Output $merged
Output
1 2 3 4 5 6
🔍

Dry Run

Let's trace merging [1, 2, 3] and [4, 5, 6] through the code

1

Define first array

$array1 = 1, 2, 3

2

Define second array

$array2 = 4, 5, 6

3

Merge arrays

$merged = $array1 + $array2 results in [1, 2, 3, 4, 5, 6]

StepActionResult
1Set $array1[1, 2, 3]
2Set $array2[4, 5, 6]
3Merge with +[1, 2, 3, 4, 5, 6]
💡

Why This Works

Step 1: Using the plus operator

The + operator in PowerShell concatenates arrays by appending the second array's elements to the first.

Step 2: Creating a new array

The merged array is a new array that holds all elements from both original arrays in order.

Step 3: Outputting the result

Using Write-Output prints each element of the merged array on its own line.

🔄

Alternative Approaches

Using array addition with @()
powershell
$merged = @($array1 + $array2)
Write-Output $merged
Wraps the result in an array explicitly, useful if inputs might not be arrays.
Using array concatenation with += operator
powershell
$merged = $array1
$merged += $array2
Write-Output $merged
Modifies the first array by adding elements of the second; good for incremental merging.
Using the .Concat() method from .NET
powershell
$merged = [System.Linq.Enumerable]::Concat($array1, $array2) | ForEach-Object { $_ }
Write-Output $merged
Uses .NET method for merging; returns an IEnumerable which PowerShell outputs as array.

Complexity: O(n + m) time, O(n + m) space

Time Complexity

Merging arrays requires copying all elements from both arrays, so time grows linearly with the total number of elements.

Space Complexity

A new array is created to hold all elements, so space usage grows with the combined size of both arrays.

Which Approach is Fastest?

Using the plus operator + is simple and efficient for most cases; .NET methods may add overhead but offer flexibility.

ApproachTimeSpaceBest For
Plus operator (+)O(n + m)O(n + m)Simple and readable merging
+= operatorO(n + m)O(n + m)Incremental merging modifying first array
.Concat() methodO(n + m)O(n + m)Using .NET methods, advanced scenarios
💡
Use $merged = $array1 + $array2 for the simplest and most readable array merge in PowerShell.
⚠️
Beginners often try to use commas inside parentheses like ($array1, $array2) which creates a nested array instead of merging.