Compare-MtJsonObject
SYNOPSISโ
Compares two PowerShell objects (typically JSON objects) and returns a list of differences.
SYNTAXโ
Compare-MtJsonObject [-Baseline] <Object> [-Current] <Object> [[-Path] <String>] [[-Settings] <Object>]
[-ProgressAction <ActionPreference>] [<CommonParameters>]
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โ
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-Table
EXAMPLE 2โ
# Exclude specific properties from comparison
$settings = [PSCustomObject]@{ ExcludeProperties = @('timestamp', 'lastModified') }
$diffs = Compare-MtJsonObject -Baseline $baseline -Current $current -Settings $settings
PARAMETERSโ
-Baselineโ
The reference object to compare against (e.g., the expected or original state).
Type: Object
Parameter Sets: (All)
Aliases:
Required: True
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
-Currentโ
The object to compare to the baseline (e.g., the actual or new state).
Type: Object
Parameter Sets: (All)
Aliases:
Required: True
Position: 2
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
-Pathโ
(Optional) The property path being compared. Used internally for recursion and reporting.
Type: String
Parameter Sets: (All)
Aliases:
Required: False
Position: 3
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
-Settingsโ
(Optional) An object that may contain an ExcludeProperties property (array of property names to skip).
Type: Object
Parameter Sets: (All)
Aliases:
Required: False
Position: 4
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
-ProgressActionโ
{{ Fill ProgressAction Description }}
Type: ActionPreference
Parameter Sets: (All)
Aliases: proga
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
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โ
OUTPUTSโ
[MtPropertyDifference[]] Returns an array of objects describing each difference found.โ
NOTESโ
Author: Stephan van Rooij @svrooij Date: 2025-06-26