Skip navigation
two stacks of round rocks

Q. Can I use PowerShell to compare text files?

Q. Can I use PowerShell to compare text files?

A. You can use Compare-Object with text files, but it doesn't work well. Generally, in a file comparison, you want the possibility for a large block of text to be different and for the files to then sync back up, which happens if someone inserts a block of text, for example, into one of the files. Compare-Object has a -syncWindow parameter that lets you specify how far out-of-sync one file can be:

Compare-Object (GC file-one.txt) (GC file-two.txt) -syncWindow 50

That will allow for either file to get out of sync for up to 50 lines. If it goes beyond that, Compare-Object will show all remaining lines as different. So it's not really an ideal text comparison, but it's the only built-in option PowerShell offers.

Do you have a Windows PowerShell question? Find more PowerShell FAQs, articles, and other resources at windowsitpro.com/go/DonJonesPowerShell.

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish