Compare Object - PowerShell


Overview

Compare-Object compares two or more objects and returns a detailed report on their similarities and differences. It’s an invaluable tool for data validation, object inspection, and debugging scripts.

Syntax

Compare-Object [-ReferenceObject] <Object1> [-DifferenceObject] <Object2>
     [-IncludeEqual] [-IncludeDifferent] [-SyncWindow <Int32>]
     [-SyncTolerance <Double>] [-ExcludeDifferent] [-IgnoreCase]
     [-Culture <String>] [-Context <Int32>] [-GroupBy <ScriptBlock>]
     [-ExcludeTypes <String[]>] [-ExcludeProperties <String[]>]
     [-Depth <Int32>] [-PassThru] [-OutputObject <FormatEnumeration>]
     [-Detailed] [-AsHashtable]
     [-Property] <Property> [-CaseSensitive] [-Force] [-WhatIf] [-Confirm]

Options/Flags

  • -ReferenceObject: Sets the primary object for comparison.
  • -DifferenceObject: Specifies the object to be compared against the reference object.
  • -IncludeEqual: Includes matching properties in the comparison report.
  • -IncludeDifferent: Includes mismatched properties in the report.
  • -SyncWindow: Tolerates small differences in dates and times.
  • -SyncTolerance: Tolerates small differences in numeric values.
  • -ExcludeDifferent: Excludes mismatched properties from the report.
  • -IgnoreCase: Compares values without regard to case.
  • -Culture: Specifies the culture to use for string comparisons.
  • -Context: Number of lines to show before and after the differences.
  • -GroupBy: Groups the comparison results by a specified property.
  • -ExcludeTypes: Excludes objects of certain types from comparison.
  • -ExcludeProperties: Excludes specific properties from comparison.
  • -Depth: Specifies the nesting level for property comparison.
  • -PassThru: Outputs the comparison result as an object.
  • -OutputObject: Specifies the output format (SideBySide, Detailed, Dictionary).
  • -Detailed: Shows detailed comparison information.
  • -AsHashtable: Returns the results as a hashtable.
  • -Property: Compares a specific property between objects.
  • -CaseSensitive: Compares property values with case sensitivity.
  • -Force: Suppresses confirmation prompts.
  • -WhatIf: Shows what the command would do without executing it.
  • -Confirm: Prompts for confirmation before executing the command.

Examples

Simple comparison without options:

Compare-Object -ReferenceObject $Object1 -DifferenceObject $Object2

Detailed comparison with equal properties excluded:

Compare-Object -ReferenceObject $Object1 -DifferenceObject $Object2 \
-IncludeDifferent -Detailed -ExcludeEqual

Grouping results by a property and excluding certain types:

Compare-Object -ReferenceObject $Object1 -DifferenceObject $Object2 \
-GroupBy {$_.Property} -ExcludeTypes 'System.*'

Common Issues

  • Ensure that the input objects have compatible types and properties.
  • Adjust -SyncWindow and -SyncTolerance values to handle time and numeric differences.
  • Use -WhatIf to preview the comparison results before executing the command.

Integration

Compare-Object can be used in combination with other PowerShell commands such as Where-Object or ForEach-Object to perform complex data analysis and manipulation tasks.

  • New-TimeSpan
  • Get-Date
  • Get-Culture