PowerTip: Add Two PowerShell Arrays Together


Summary: Easily add two Windows PowerShell arrays together.

Hey, Scripting Guy! Question If I have an array stored in one variable, and another array stored in another variable, how can I add them together?

Hey, Scripting Guy! Answer Use the + operator to add to arrays together:

PS C:\> $a = 2,3,4

PS C:\> $b = 5,6,7

PS C:\> $c = $a + $b

PS C:\> $c

2

3

4

5

6

7

PS C:\>

Comments (2)

  1. Missing from this is the related question of how to do this with distinct results, similar to the SQL "Select Distinct" command. i.e, if both of the above lists contained "1", how do i return a list that contains "1" only once.

  2. nail says:

    $c = $a + $b | select -uniq

Skip to main content