How to combine powershell objects ? The fusion sounds like a good idea right ? That is cool but it ‘might’ be a bit time consuming if whe need to learn the dance and stuff 😉

Read below to see how I have done it.

[stextbox id=”note” defcaption=”true”]You can only understand this if you are a DBZ fan (just like me ;))[/stextbox]

How to combine powershell objects ?

This is a question I have recently asked my self. I came up pretty quickly with the Combine-Objects function which allow to concatenate powershell custom objects, or objects that have been returned from WMI or AD for example.

[stextbox id=”download” caption=”Download”]Download the function fastly and directly from Technet right here –> Download script (Don’t forget to rate by clicking on the yellow stars ;))[/stextbox]

Script Listing:


By | 2014-11-19T12:36:29+00:00 October 28th, 2014|Functions, PowerShell|10 Comments

About the Author:

Stéphane is a dynamic and passionate Cloud and datacenter Microsoft MVP since. He is the founder of the Basel PowerShell user Group (BPUG), the co-founder of the French Speaking PowerShell UserGroup (FRPSUG), author, blogger, and received the community award "PowerShell Hero" from Stéphane has implemented microsoft infrastructure solutions in various countries of Europe and is currently working in Basel / Switzerland. Stéphane help his clients to reduce their global infrastructure costs by implementing Microsft infrastructure solutions by combining great products such as System Center, Windows Server, with heavy automation using Windows PowerShell. Stéphane loves languages, Belgium beer, French cheese and French Wine. If any of these topics are of your interest, don't hesitate to come and say hi.


  1. Sam March 27, 2019 at 4:02 pm - Reply

    Late to this thread, but Peter’s function works really well. One addition I made, which makes it only compatible with Powershell v3, is to replace $ResultHash = @{} with $ResultHash = [ordered] @{}. This will order the results in the same order that they were in the input object. The original one will spit them out in random order.

  2. pascal February 11, 2019 at 6:15 pm - Reply

    Little modification for this one, it will work when merging AD user object:

    function merge-object {

    Param (

    $arguments = [Pscustomobject]@()

    foreach ( $Property in ($Object1 | Get-Member -MemberType Properties) ){
    $arguments += @{$Property.Name = $Object1.($ }


    foreach ( $Property in ($Object2 | Get-Member -MemberType Properties) ){
    $arguments += @{ $Property.Name = $Object2.($ }


    $Object3 = [Pscustomobject]$arguments

    return $Object3

  3. David March 8, 2017 at 7:01 am - Reply

    I got an Object[] back with each element being a HashTable?

    I found that this code works better:
    function Combine-Objects

    $object3 = New-Object -TypeName PSObject

    foreach ( $Property in $Object1.PSObject.Properties)
    $arguments += @{$Property.Name = $Property.value }

    $object3 | Add-Member -MemberType NoteProperty -Name $Property.Name -Value $Property.value

    foreach ( $Property in $Object2.PSObject.Properties)
    $object3 | Add-Member -MemberType NoteProperty -Name $Property.Name -Value $Property.value

    return $object3

    • Chris Magnuson August 18, 2017 at 9:20 pm - Reply

      This is what I needed, thanks David!

      • Chris Magnuson August 18, 2017 at 9:46 pm - Reply

        Using David’s code I have modified this to create a Merge-Object function to use with a piped an array of piped in objects ($ArrayOfObjects | Merge-Object):

        function Merge-Object {
        begin {
        $MergedObject = New-Object -TypeName PSObject

        process {
        foreach ($Property in $Object.PSObject.Properties) {
        $MergedObject | Add-Member -MemberType NoteProperty -Name $Property.Name -Value $Property.value
        end {

  4. Francois-Xavier Cat October 31, 2014 at 4:45 pm - Reply

    I did not know you where a Manga/Anime Fan 🙂
    I am as well (just finished watching HunterxHunter)

  5. Stephane October 29, 2014 at 3:30 pm - Reply

    That is a great improvement Peter!! Great work !

  6. Peter Kriegel October 29, 2014 at 2:39 pm - Reply

    Very inspiring!
    I have improved it a bit! 😉

    Function Combine-Object {

    Param (
    [Parameter(Mandatory = $true)]
    [String[]]$Property = @()

    # query if the user has given property namens to select from the Objects
    # for performance reasons we do this query outside of the ForEach loop
    $FilterProperties =$false
    If($Property.count -gt 0){$FilterProperties = $True}

    # to avoid the use of the slow speed += operator or the Add-Member cmdlet
    # we are using a hashtabel here
    # create empty hashtable to hold the properties for the resulting Object
    $ResultHash = @{}

    # process each object from the InputObject array
    ForEach($InObject in $InputObject) {
    # process each property from the current processed object
    ForEach ( $InObjProperty in $InObject.psobject.Properties){
    # If the user which to select only given Properties
    If($FilterProperties) {
    # add only selected property to the hashtable
    If($Property -contains $InObjProperty.Name) {
    $ResultHash.($InObjProperty.Name) = $InObjProperty.value
    } Else {
    # user do not want to filter down the Properties out of the object
    # so we add all properties from the objects to the hashtable
    # ATENTION! If a Property from one Object has the same Name like another Property
    # this will result in a non terminating error!
    $ResultHash.($InObjProperty.Name) = $InObjProperty.value

    # create (and return) the resulting Object from from the hashtable
    # this is the PowerShell 2.0 compatible and fast way to create an custom object!
    New-Object -TypeName PSObject -Property $ResultHash

    Peter Kriegel
    owner of the German speaking PowerShell Community

  7. Øyvind Kallstad October 28, 2014 at 3:10 pm - Reply

    This is really cool! Simple but effective, as PowerShell should be. Thanks.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: