Skip to main content

Determine the Differences Between Two Files

Problem

You want to determine the differences between two files.

Solution

To determine simple differences in the content of each file, store their content in variables, and then use the CompareObject cmdlet to compare those variables:

PS >"Hello World" > c:\temp\file1.txt PS >"Hello World" > c:\temp\file2.txt PS >"More Information" >> c:\temp\file2.txt PS >$content1 = GetContent c:\temp\file1.txt PS >$content2 = GetContent c:\temp\file2.txt PS >CompareObject $content1 $content2

InputObject SideIndicator

More Information =>

Discussion

The primary focus of the CompareObject cmdlet is to compare two unordered sets of objects. Although those sets of objects can be strings (as in the content of two files), the output of CompareObject when run against files is usually counterintuitive due to the content losing its order.

When comparing large files (or files where the order of comparison matters), you can still use traditional file comparison tools such as diff.exe or the WinDiff application that comes with both the Windows Support Tools and Visual Studio.

For more information about the CompareObject cmdlet, type GetHelp CompareObject.

If you need dedicated infrastructure for comparing and analyzing files, consider a Spain Dedicated Server for greater control over file management, data processing, system utilities, server administration, and demanding application workloads.

Help Category