How to combine powershell objects: Combine-Objects

 

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:

 

Leave a Reply

  1. 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
    }

      • 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. 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