Joining property sets from two objects


Anyone happen to know if there's a neat way to combine the property sets of two different objects?

I ended up with this to add the properties of one to another since I couldn't find anything immediately obvious. Is there something better?

Chris
function Join-Object ($BaseObject, $AdditionalObject) {
  ForEach ($Property in $($AdditionalObject | Get-Member -Type Property)) {
    $BaseObject | Add-Member -MemberType NoteProperty -Name $Property.Name `
      -Value $AdditionalObject.$($Property.Name) -ErrorAction SilentlyContinue
  }
  return $BaseObject
}

Open in new window

LVL 71
Chris DentPowerShell DeveloperAsked:
Who is Participating?
 
BSonPoshCommented:
I think that is best way.

You could perhaps use Select-Object, but your way is the cleanest that I can think of.
0
 
Chris DentPowerShell DeveloperAuthor Commented:

Cool, thank you, I'll stick with that one then

The prospect of writing 40 @{n=...;e=...} statements to end up with the combined set was about as unappealing as trimming the property set to a few that may or may not be interesting.

Chris
0
 
Chris DentPowerShell DeveloperAuthor Commented:
Thank you, hope you have a good weekend :)

Chris
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.