Diff Two Text Files with Compare-Object

Built-in PowerShell comparison that lists lines unique to each file.

WindowsdifftextpowershellLow risk
Run `Compare-Object (Get-Content a.txt) (Get-Content b.txt)` to list lines that exist in only one of the two files.

01Command

Command
$ Compare-Object (Get-Content a.txt) (Get-Content b.txt) -IncludeEqual:$false

02What it does

Compare-Object compares the line arrays produced by Get-Content and prints lines with a SideIndicator: => means the line is only in the second file, <= only in the first. Adding -CaseSensitive matters for config files, and -SyncWindow 0 forces strict line-position comparison instead of set matching. For a full unified-diff feel, pipe the result through Format-Table or select LineInputObject.

03Risk

RiskLow risk
!Warning: Compare-Object matches lines as a set by default, so duplicated lines are grouped; use -SyncWindow 0 for order-sensitive comparison.

04Undo

✓No undo needed — this command does not change persistent state.

05Sources

  • Compare-Object (Microsoft.PowerShell.Utility)
    Microsoft Learn · vendor-doc · 2026-09-17
    Evidence: Documents Compare-Object with => (present only in reference object) and <= (present only in difference object) side indicators.

Last verified: 2026-09-17 · Category: Files & Folders

06Related solutions