Q. Can I use PowerShell to compare text files?

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.

Don Jones

August 9, 2010

1 Min Read
two stacks of round rocks

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.

 

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like