Merge Custom PSObjects

This topic contains 2 replies, has 2 voices, and was last updated by  Grant Harrington 3 months, 3 weeks ago.

  • Author
    Posts
  • #76178

    Grant Harrington
    Participant

    I've been searching, but not finding a solution that's working

        $Props01 = [ordered]@{
            'Prop01A' = 'Prop01A_Value'
            'Prop01B' = 'Prop01B_Value'
        } #end $Props01
        $Obj01 = New-Object -TypeName PSObject -Property $Props01
    
        $Props02 = [ordered]@{
            'Prop02A' = 'Prop02A_Value'
        } #end $Props02
        $Obj02 = New-Object -TypeName PSObject -Property $Props02

    Obj01 contains:
    Prop01A
    Prop01B

    Obj02 contains
    Prop02A

    I'd like to be able to make Obj03 which contains:
    Prop01A
    Prop01B
    Prop02A

    I know I can mix/match and place all three in a single hashtable, but I'm looking at having dozens of these custom objects "groups" and want to be able to pick and choose which Obj0x gets merged with Obj0y.

    $Props03 = [ordered]@{
    'Prop01A' = 'Prop01A_Value'
    'Prop01B' = 'Prop01B_Value'
    'Prop02A' = 'Prop02A_Value'
    }

    Thanks.

  • #76180

    Don Jones
    Keymaster

    Yeah, there's not a "merge." That really goes against the original concepts of object-oriented programming, which is why; objects aren't simply "data structures," although in PowerShell we admittedly use them that way a lot. The most you could probably do is write a Merge-Object command which programmatically does this for you.

  • #76187

    Grant Harrington
    Participant

    Thank you for that explanation. I kept searching after my post, and this link provides the solution I was looking for.

    http://powershelldistrict.com/how-to-combine-powershell-objects/ (See post by David, 3/8/2017).

You must be logged in to reply to this topic.