PowerTip: Add Two PowerShell Arrays Together

Doctor Scripto

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

1 comment

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

  • Alaa Elmahdy 0

    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}}

Feedback usabilla icon