Functions/Compare-ActiveDirectoryGroups.ps1

<#
.SYNOPSIS
    This function compares two Active Directory group objects and returns if the objects are equivalent.
#>

function Compare-ActiveDirectoryGroups {
    [CmdletBinding(PositionalBinding=$false)]
    [OutputType([Boolean])]
    param (
        # The Active Directory group used as the reference object.
        [Parameter(Mandatory=$true)]
        [ValidateNotNull()]
        [PSObject]$referenceGroup,

        # The Active Directory group used as the comparison object.
        [Parameter(Mandatory=$true)]
        [ValidateNotNull()]
        [PSObject]$comparisonGroup,

        # Select the stream where the messages will be directed.
        [Parameter(Mandatory=$false)]
        [ValidateSet("Information", "Warning", "Error", "None")]
        [String]$outputStream = "Error"
    )

    # Get list of properties to compare
    $propertiesToCompare = Get-ActiveDirectoryGroupPropertyList

    # Compare properties
    foreach ($property in $propertiesToCompare) {
        if ($referenceGroup.$property -ne $comparisonGroup.$property) {
            Write-OutputMessage -Message "The '$($property)' property does not match for group '$($referenceGroup.Name)' - Reference: '$($referenceGroup.$property)' Comparison: '$($comparisonGroup.$property)'" -OutputStream $outputStream -ReturnMessage:$false
            return $false
        }
    }

    # Compare the lists of members
    if ($null -ne (Compare-Object -ReferenceObject (ConvertTo-Array $referenceGroup.Members) -DifferenceObject (ConvertTo-Array $comparisonGroup.Members))) {
        Write-OutputMessage -Message "The member list does not match for group '$($referenceGroup.Name)' - Reference: '$($referenceGroup.Members)' Comparison: '$($comparisonGroup.Members)'" -OutputStream $outputStream -ReturnMessage:$false
        return $false
    }

    # The two objects are the same
    return $true
}