1

enter image description here

I am new to powershell. I have 2 pscustomobjects in powershell script. I want to bring them together as one and output into csv.

The problem is there is no key column to match in both, just want to append them together. I was able to use the following code but it does not show the data correctly. Any Suggestions? (sample attached)

$result=[pscustomobject]($a+$b)
Harry
  • 93
  • 8
  • Could you share a simple example of `$a` and `$b` and how would expect `$result` to look like? – Santiago Squarzon Dec 08 '22 at 16:55
  • If values are numbers : $result=[pscustomobject]([int]$a+[int]$b) – jdweng Dec 08 '22 at 17:00
  • @SantiagoSquarzon I attached the sample to question. – Harry Dec 08 '22 at 17:20
  • @jdweng values are mostly string. – Harry Dec 08 '22 at 17:29
  • Then cast to string instead of int. – jdweng Dec 08 '22 at 17:32
  • Using this [`Join-Object script`](https://www.powershellgallery.com/packages/Join)/[`Join-Object Module`](https://www.powershellgallery.com/packages/JoinModule) (see also: [How to merge two csv files by keeping the duplicate column name in Powershell](https://stackoverflow.com/a/61609920/1701026) and [In Powershell, what's the best way to join two tables into one?](https://stackoverflow.com/a/45483110/1701026)): `$Result = $a |Join $b` – iRon Dec 09 '22 at 09:59

2 Answers2

1

It seems what you're looking to do is merge the properties from both objects into one.

You can access the properties and property values of an object via the intrinsic member PSObject, in this case you would need to enumerate all properties from both objects ($a and $b) and put them together into an ordered dictionary and lastly cast [pscustomobject] to it to get a new merged object as a result. This function can simplify that process for you.

function Merge-PSCustomObject {
    param(
        [Parameter(Mandatory)]
        [Object] $LeftObject,

        [Parameter(Mandatory)]
        [Object] $RigthObject
    )

    end {
        $result = [ordered]@{}
        foreach($property in @($LeftObject.PSObject.Properties; $RigthObject.PSObject.Properties)) {
            $result[$property.Name] = $property.Value
        }
        [pscustomobject] $result
    }
}

$a = [pscustomobject]@{ foo = 'hello' }
$b = [pscustomobject]@{ bar = 'world'; baz = 123 }

Merge-PSCustomObject -LeftObject $a -RigthObject $b

If you have an array of objects in both sides, assuming both arrays have the same count of objects, you can use a for loop to merge each object in both arrays, for example:

$a = [pscustomobject]@{ foo = 'hello' }
$b = [pscustomobject]@{ bar = 'world'; baz = 123 }

$arr1 = 0..3 | ForEach-Object { $a }
$arr2 = 0..3 | ForEach-Object { $b }

$result = for($i = 0; $i -lt $arr1.Count; $i++) {
    Merge-PSCustomObject $arr1[$i] $arr2[$i]
}
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
  • I got following output instead of table: Count Length LongLength Rank SyncRoot IsReadOnly IsFixedSize IsSynchronized 12 12 12 1 System.Object[] FALSE TRUE FALSE – Harry Dec 08 '22 at 18:04
  • @Harry this function works for single objects on both sides it's not meant to merge arrays of objects – Santiago Squarzon Dec 08 '22 at 18:07
  • @Harry see the update, that should work for merging both arrays assuming they have the same count of objects. – Santiago Squarzon Dec 08 '22 at 18:11
  • 1
    This is what I was looking for! Instead of using $arr1 = 0..3 | ForEach-Object { $a } I sis a count on first array object and then used it. Works perfect! Appreciate your help. – Harry Dec 08 '22 at 19:05
1

See if following works :

for($i = 0; $i -lt $a.Count; $i++)
{
   $a[$i] | Add-Member -NotePropertyName Sample3 -NotePropertyValue $b[$i].Sample3
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • I did try this method but for some reason sample3 table was not showing up at all. I tried the function from @Santiago Squarzon and it worked. Thank you for the help! – Harry Dec 08 '22 at 19:04