Compare All Properties of an Object

As Exchange Online administrators, my team and I frequently utilized Microsoft's Hybrid Configuration Wizard. However, we discovered that the wizard occasionally altered our customized settings, which was quite frustrating. To mitigate this issue, we developed a process of storing configurations by running specific commands and then manually comparing the properties after the wizard completed. Realizing the need for a more foolproof approach, we decided to implement an automated method for comparing all properties, thereby ensuring complete accuracy and minimizing the risk of errors.

PowerShell's Compare-Object cmdlet allows you to compare two objects by their properties, but its functionality is limited to a specific set of parameters. To overcome this limitation, our team developed a custom function that encapsulates Compare-Object within a loop to handle all properties of both objects or collections. However, the crux of our approach was to identify and extract every property of the objects, which enabled us to make comprehensive and accurate comparisons.

We welcome your feedback and suggestions for improvement. If you notice any errors or have any recommendations, please feel free to leave a comment below.

Function Compare-Object_AllProps{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [Object]$Before,
        [Parameter(Mandatory=$true)]
        [Object]$After
    )

    $BeforeProps = $Before[0].psobject.Properties.Name
    $AfterProps = $After[0].psobject.Properties.Name
    $PropCompare = Compare-Object -ReferenceObject $BeforeProps -DifferenceObject $AfterProps

    If($PropCompare){
        Write-Output "Objects do not have similar property names"
        Exit
    }

    If($BeforeProps -Contains "Name"){$ID = "Name"}
    If($BeforeProps -Contains "Identity"){$ID = "Identity"}

    $Index = 0
    Foreach($item in $Before){
        $Ref = $item
        $Dif = $After[$Index]
        Foreach($Property in $BeforeProps){
            $Compare = Compare-Object -ReferenceObject $Ref -DifferenceObject $Dif -Property $Property
            If($Compare){
                Write-Output "Identity: $($Item.$ID)"
                Write-Output "Property: $Property"
                Write-Output "Before:   $(($Compare | Where-Object {$_.SideIndicator -eq "<="}).$Property)"
                Write-Output "After:    $(($Compare | Where-Object {$_.SideIndicator -eq "=>"}).$Property)"
                Write-Output ""
            }
        }
        $Index++
    }
}