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|7 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 PowerShell.org. 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.

7 Comments

  1. 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
    {
    param([Parameter(Mandatory=$true)][PSCustomObject]$Object1,
    [Parameter(Mandatory=$true)][PSCustomObject]$Object2)

    $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 {
        param(
        [Parameter(Mandatory,ValueFromPipeline)][PSCustomObject]$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 {
        $MergedObject
        }
        }

  2. 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)

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

    That is a great improvement Peter!! Great work !

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

    Very inspiring!
    I have improved it a bit! 😉


    Function Combine-Object {

    [CmdletBinding()]
    Param (
    [Parameter(Mandatory = $true)]
    [ValidateCount(2,2147483646)]
    [PSObject[]]$InputObject,
    [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
    }

    greets
    Peter Kriegel
    owner of the German speaking PowerShell Community
    http://www.PowerShell-Group.eu

  5. Ø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

%d bloggers like this: