Contatenate 2 differents psobject

Hello,

I try to concatenate 2 differents PSObject.

Example :

$x = @()
$x += [pscustomobject]@{ idA = 1 ; date1 = "20/03/2001" ; valA = "test1" }
$x += [pscustomobject]@{ idA = 1 ; date1 = "21/03/2001" ; valA = "test2" }
$y = @()
$y += [pscustomobject]@{ idB = 1 ; date2 = "20/03/2001" ; valB = "test3" }
$y += [pscustomobject]@{ idB = 1 ; date2 = "23/03/2001" ; valB = "test4" }

I’d like to get another object like that.

idA date1 valA     idB date2 valB
--- ----- ----     --- ----- ----
1 20/03/2001 test1
1 21/03/2001 test2
___________________ 1 20/03/2001 test3
___________________ 1 23/03/2001 test4

I found “join-object” or “combine-objects” functions but I haven’t key 1<=>1 needed to join data.

I try to found inspiration from https://www.reddit.com/r/PowerShell/comments/62gcdn/combining_two_ps_object_values/ but I can’t found an “universal” method to do this quickly.

Maybe some solution with LINQ ?

any tips to do this efficiently ?

 

thank you for your help !

Typically, objects are not joined like this as there is key that is being joined on which is the links you mentioned above. Basically, you can create another object and add the two objects, but you have to use the Select-Object with all of the properties or it will only show the properties of the first object added:

$x = @()
$x += [pscustomobject]@{ idA = 1 ; date1 = "20/03/2001" ; valA = "test1" }
$x += [pscustomobject]@{ idA = 1 ; date1 = "21/03/2001" ; valA = "test2" }
$y = @()
$y += [pscustomobject]@{ idB = 1 ; date2 = "20/03/2001" ; valB = "test3" }
$y += [pscustomobject]@{ idB = 1 ; date2 = "23/03/2001" ; valB = "test4" }

$obj= @()
$obj += $x
$obj += $y

$obj | Select IdA,IdB,date1,date2,valA,valB

Continuing from Rob’s answer. If you don’t want to hard code the property names, you can use this technique to get them all.

Steps are take each set of objects, get the first item from each, gather the property names.

$x = @()
$x += [pscustomobject]@{ idA = 1 ; date1 = "20/03/2001" ; valA = "test1" }
$x += [pscustomobject]@{ idA = 1 ; date1 = "21/03/2001" ; valA = "test2" }
$y = @()
$y += [pscustomobject]@{ idB = 1 ; date2 = "20/03/2001" ; valB = "test3" }
$y += [pscustomobject]@{ idB = 1 ; date2 = "23/03/2001" ; valB = "test4" }

$props = $x,$y | foreach {$_ | select -first 1 | foreach {$_.psobject.properties.name}}

$obj = $x + $y | select $props 

$obj contents

idA   : 1
date1 : 20/03/2001
valA  : test1
idB   : 
date2 : 
valB  : 

idA   : 1
date1 : 21/03/2001
valA  : test2
idB   : 
date2 : 
valB  : 

idA   : 
date1 : 
valA  : 
idB   : 1
date2 : 20/03/2001
valB  : test3

idA   : 
date1 : 
valA  : 
idB   : 1
date2 : 23/03/2001
valB  : test4

Since it’s more than 4 properties, powershell formatting defaults to list. To show in a table you can add | Format-Table

$obj | ft

idA date1      valA  idB date2      valB 
--- -----      ----  --- -----      ---- 
  1 20/03/2001 test1                     
  1 21/03/2001 test2                     
                     1   20/03/2001 test3
                     1   23/03/2001 test4