PowerShell Tutorial: Using Compare-Object With Files

in STEMGeeks4 years ago

One common task in troubleshooting is comparing two entities. These can be environments, servers, files, or other objects. There's a myriad of tools we can use, but one built-in tool to Windows platforms is PowerShell and it comes with a useful compare function. In the video, PowerShell: Compare-Object Function, we look at using this built-in function by comparing two files - a task we often do when comparing two configuration files of environments where functionality works in one, but not the other.

In the video, we look at solving the possible issues with this built-in function:

  • Understanding the SideIndicator and what this means when we look at two objects - in this case files, but keep in mind that we may be looking at two environment variables, etc.
  • How to filter for only looking at the differences between the objects. In many cases with troubleshooting, we only want to return what's different because what's equal doesn't matter. When looking for differences, we see that PowerShell makes this the default option when using this built-in function.
  • How to include what's equal along with what differs - in this case, with the files. In fewer cases, we may want to know what's different and what's the same.
  • A quick note about how we may want to filter further, relative to the output, such as ordering the output.
A practical use case of this is comparing two script files, one which functions correctly and other which does not. Suppose that the files differ for some legitimate reasons, such as specific configurations, but we find that one runs correctly, while the other doesn't (I've seen this multiple times). This Compare-Object function will help us identify the differences and evaluate if those are legitimate or whether the differences may explain why one functions but the other does not.

While we can find many helpful tools to compare files or other objects, PowerShell comes with this operation built-in, so provided you can read the output, you'll already have a tool at your disposal to use.

Sort:  

Congratulations @sqlinsix! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

You received more than 1500 upvotes.
Your next target is to reach 1750 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Support the HiveBuzz project. Vote for our proposal!