April 27th, 2013

PowerTip: Add Two PowerShell Arrays Together

Doctor Scripto
Scripter

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:\>

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

1 comment

Discussion is closed. Login to edit/delete existing comments.

  • Alaa Elmahdy

    you can use this
    function Join-Arrays ($RightArray,$LeftArray,$RightJoinOn,$LeftJoinOn){$ResultsArray =@()foreach ($Right in $RightArray){$Left= $LeftArray |where {$_.$LeftJoinOn -eq $Right.$RightJoinOn}if ($Right.$RightJoinOn -eq $Left.$LeftJoinOn){$obj = New-Object psobject$Left.psobject.properties |% { $obj | Add-Member -MemberType NoteProperty -Name $_.Name -Value $_.Value }$Right.psobject.properties |% { $obj | Add-Member -MemberType NoteProperty -Name $_.Name -Value $_.Value -Force}$ResultsArray +=$obj}}