Compare-MtJsonObject
SYNOPSIS
Section titled “SYNOPSIS”Compares two PowerShell objects (typically JSON objects) and returns a list of differences.
SYNTAX
Section titled “SYNTAX”Compare-MtJsonObject [-Baseline] <Object> [-Current] <Object> [[-Path] <String>] [[-Settings] <Object>] [-ProgressAction <ActionPreference>] [<CommonParameters>]DESCRIPTION
Section titled “DESCRIPTION”The Compare-MtJsonObject function recursively compares two objects (such as those imported from JSON) and returns an array of differences. It supports comparison of nested objects, arrays, and allows exclusion of specific properties via the Settings parameter. The function is useful for configuration drift detection, regression testing, or validating changes between baseline and current states.
EXAMPLES
Section titled “EXAMPLES”EXAMPLE 1
Section titled “EXAMPLE 1”# Compare two JSON files and output the differences$baseline = Get-Content -Raw -Path 'baseline.json' | ConvertFrom-Json$current = Get-Content -Raw -Path 'current.json' | ConvertFrom-Json$diffs = Compare-MtJsonObject -Baseline $baseline -Current $current$diffs | Format-TableEXAMPLE 2
Section titled “EXAMPLE 2”# Exclude specific properties from comparison$settings = [PSCustomObject]@{ ExcludeProperties = @('timestamp', 'lastModified') }$diffs = Compare-MtJsonObject -Baseline $baseline -Current $current -Settings $settingsPARAMETERS
Section titled “PARAMETERS”-Baseline
Section titled “-Baseline”The reference object to compare against (e.g., the expected or original state).
Type: ObjectParameter Sets: (All)Aliases:
Required: TruePosition: 1Default value: NoneAccept pipeline input: FalseAccept wildcard characters: False-Current
Section titled “-Current”The object to compare to the baseline (e.g., the actual or new state).
Type: ObjectParameter Sets: (All)Aliases:
Required: TruePosition: 2Default value: NoneAccept pipeline input: FalseAccept wildcard characters: False(Optional) The property path being compared. Used internally for recursion and reporting.
Type: StringParameter Sets: (All)Aliases:
Required: FalsePosition: 3Default value: NoneAccept pipeline input: FalseAccept wildcard characters: False-Settings
Section titled “-Settings”(Optional) An object that may contain an ExcludeProperties property (array of property names to skip).
Type: ObjectParameter Sets: (All)Aliases:
Required: FalsePosition: 4Default value: NoneAccept pipeline input: FalseAccept wildcard characters: False-ProgressAction
Section titled “-ProgressAction”Determines how PowerShell responds to progress updates generated by a script, cmdlet, or provider, such as the progress bars generated by Write-Progress.
Type: ActionPreferenceParameter Sets: (All)Aliases: proga
Required: FalsePosition: NamedDefault value: NoneAccept pipeline input: FalseAccept wildcard characters: FalseCommonParameters
Section titled “CommonParameters”This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.
INPUTS
Section titled “INPUTS”OUTPUTS
Section titled “OUTPUTS”[MtPropertyDifference[]] Returns an array of objects describing each difference found.
Section titled “[MtPropertyDifference[]] Returns an array of objects describing each difference found.”Author: Stephan van Rooij @svrooij Date: 2025-06-26