Comparing Files In PowerShell - 2 Approaches
Tags: powershell, tutorial, troubleshooting
Published: 2021-05-27 09:20:04

Comparing files for similarities and differences will come up frequently. If we're running code in different environments from the same code base, but our configurations should contain differences, we may want to compare the configuration files to ensure that all other configurations remain the same except environmental configurations. Likewise, if we're troubleshooting a log issue in one environment that has slight nuances to another environment, comparing the log files may be helpful. This also applies to comparing code, such as SQL code running in two different environments or .NET code from different repositories. How we approach comparing files depends on what we're trying to achieve. In the below videos, we'll see two different approaches and we look at some examples of where we may want to use either.

We can see this comparison of comparing files in the two videos of [PowerShell: Compare-Object Function](https://www.youtube.com/watch?v=7Fj-jICazGc) and [Another Approach To Comparing Files In PowerShell (and Where To Use It)](https://www.youtube.com/watch?v=x0gePcHK6ww). In the first video, we're looking at a built-in function that's automatically provided. In the second video, we're looking at a custom function that outputs differences between two files on a starting-line basis. A starting-line basis means that any difference from the starting line is highlighted; this does not perform a line join and find where there's matches, but finds where there are similarities from the starting point. This means that if lines 1, 2 and 3 match, but the remaining lines are offset by a new line, this will report that everything after 3 differs.

When would we want to use the first or second function?

- The first function of Compare-Object (from the first video) is useful if we want to see only the differences between files with the possibility of being able to see the similarities. This function will perform a line-join in that it will find all the lines that match in the files, while also finding all the lines that don't match in the files and indicate where they match and don't match if we add the appropriate option.
- The second function of Compare-MSSQLObjects (from the second video) is useful when we want to highlight differences by the starting point of a file. What we're really looking for when we use this function is the FIRST difference between files. Take a two file example where both files have the same lines 1, 2, 3, 4, but line 5 differs with the first file having a line of "5" and the second file having a "6", then both files having the same order to number 7 with nothing else. With this function, starting at line 5 it would report the file as different from that point on, even though both files would contain 7. The big difference is that 7 doesn't occur on the same line number, even if it's present in both files.
- Are we ultimately looking for differences and similarities or are we looking for first differences that would cause the issue. In the first case, we'd want Compare-Object whereas in the second case, Compare-MSSQLObjects would be faster.

Keep in mind, that the Compare-MSSQLObjects outputs a file, so if you're needing this on a screen, you would update the function as appropriate.

If you'd like to use the function in the seccond video, the GitHub link is in the video's description. This was ultimately designed as a tool to detect first differences in T-SQL code, but it would just as easily apply to log files, code in general, etc. The first video's function is already built into PowerShell, so it has many advantages in that you don't need to copy code. Both can be useful depending on the situation, as we may need a report or we may want to know where files differ by starting point.