Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have two text files and want to find the differences between them using Windows Powershell. Is there something similar to the Unix diff tool available? Or is there another other way I haven't considered?

I've tried compare-object, but get this cryptic output:

PS C:\> compare-object one.txt two.txt

InputObject                                                 SideIndicator
-----------                                                 -------------
two.txt                                                     =>
one.txt                                                     <=
share|improve this question
up vote 55 down vote accepted

Figured it out myself. Because Powershell works with .net objects rather than text, you need to use get-content to expose the contents of the text files. So to perform what I was trying to do in the question, use:

compare-object (get-content one.txt) (get-content two.txt)
share|improve this answer
I was very surprised when I tried to compare two files: an unsorted array of numbers, and the same array of numbers after sorting them. There is no output despite the files being very different. Apparently, compare-object doesn't consider order. – cgmb Feb 4 '15 at 6:58
@cgmb - You can use -SyncWindow 0 to fix that, I believe, though I'm unsure if it's only recently been introduced. It's not particularly smart about it, though. – James Ruskin Jan 28 at 14:49

Or you could use the DOS 'FC' command like so (This shows the output of both files so you will have to scan for the differences):

fc.exe filea.txt fileb.txt > diff.txt

fc is an aliase for the Format-Custom cmdlet so be sure to enter the command as fc.exe. Please note that many DOS utilities don't handle UTF-8 encoding

You can also spawn a CMD process and run fc within it.

start cmd "/c ""fc filea.txt fileb.txt >diff.txt"""

This instruct PowerShell to start a process with the 'cmd' program using the parameters in quotes. In the quotes, is the '/c' cmd option to run the command and terminate. the actual command to run by cmd in the process is "fc filea.txt fileb.txt" redirecting the output to the file diff.txt.

You can use the DOS fc.exe from within powershell.

share|improve this answer
+1 for bring out the DOS ^_^ – Jeff Bridgman Apr 17 '13 at 15:21

A simpler way of doing it is to write:

diff (cat file1) (cat file2)
share|improve this answer
Diff and cat are just aliases for Compare-Object and Get-Content in PowerShell. It is the same thing. – Shawn Melton Feb 10 '12 at 1:54

diff on *nix is not part of the shell, but a separate application.

Is there any reason you can't just use diff.exe under PowerShell?

You can download a version from the UnxUtils package (

share|improve this answer
Because PowerShell is included now, nothing to download and install. – Bratch Jul 31 '12 at 16:34

WinMerge is another good GUI-based diff tool.

share|improve this answer
This is how I did it in the past, which is a manual process, that I wanted to replace with a small script. – Bratch Jul 31 '12 at 16:35

compare-object (aka diff alias) is pathetic if you expect it to behave something like a unix diff. I tried the diff (gc file1) (gc file2), and if a line is too long, I can't see the actual diff and more importantly, I can't tell which line number the diff is on.

When I try adding -passthru, I now can see the difference, but I lose which file the difference is in, and I still don't get a line number.

My advice, don't use powershell to find differences in files. As someone else noted, fc works, and works a little better than compare-object, and even better is downloading and using real tools like the unix emulator that Mikeage mentioned.

share|improve this answer

As others have noted, if you were expecting a unix-y diff output, using the powershell diff alias would let you down hard. For one thing, you have to hold it's hand in actually reading files (with gc / get-content). For another, the difference indicator is on the right, far from the content -- it's a readability nightmare.

The solution for anyone looking for a sane output is

  1. get a real diff (eg from GnuWin32)
  2. edit %USERPROFILE%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
  3. add the line

    remove-item alias:diff -force

The -force argument is required because Powershell is quite precious about this particular inbuilt alias. If anyone is interested, having GnuWin32 installed, I also include the following in my powershell profile:

remove-item alias:rm
remove-item alias:mv
remove-item alias:cp

Mainly because Powershell doesn't understand arguments which are run together and typing, for example "rm -Force -Recurse" is a lot more effort than "rm -rf".

Powershell has some nice features, but there are some things it should just not try to do for me.

share|improve this answer

There's also Windiff which provides a GUI diff interface (great for use with GUI based CVS/SVN programs)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.