PowerShell Compare-Object

Compare-Object to Find the Differences Between Files

The main job of this cmdlet is produce a list of items missing from the reference file.

Topics for PowerShell Compare-Object Cmdlet

 ♣

Preparation: Use PowerShell’s Built-In Help

This script is a useful way of checking Compare-Object’s parameters.

# Example of PowerShell Compare-Object
# Author: Guy Thomas
Help Compare-Object -full

Note 1: With the Compare-Object cmdlet, there are two required parameters -referenceObject and -differenceObject.  For most examples -referenceObject controls the master content, the file which holds ALL the information, while -differenceObject has the secondary, or ‘after the event’ file.  Also note what help says about these parameters, it tells us that the position of these parameters is important, reference list first, difference list second.

Note 2: In a production script we employ Get-Content to open the file, read the text, and then assign the content to a variable.  For example $strReference = Get-Content "C:\Scripts\list.txt"

Note 3: Please investigate more parameters such as -IncludeEqual and -syncWindow.  Incidentally, PowerShell has an alias for Compare-Object called Diff.

Example 1: To Compare Two Files, and List Their Differences

For the sake of clarity, and so that we focus on Compare-Object, I have scaled down our mission to tackle only comparing two files.  The best way to understand this example is to pretend that we have a shopping list and a till checkout receipt; our task is to compare the lists and thus find the items we forgot to buy.

Preparation
We need two files, the first is for reference and the second for comparison.  Let us use a shopping list for our test scenario.  The file called ShopList.txt is a list of goods we need to buy at the supermarket.  Checkout.txt, is a list of what we actually purchased.

ShopList: Save these 10 items into a text file, I called mine C:\temp\ShopList.txt

®

Oranges
Bananas
Cucumbers
Beer
Burgers
Onions
Sausages
Steak
Chips

Checkout: Save these 7 items into a text file, I called mine C:\temp\Checkout.txt

Oranges
Bananas
Beer
Sausages
Steak
Chips
Ice-Cream

Now we are ready to execute this Compare-Object script.  My favoured method is to save these instructions into a cmdlet with a .ps1 extension, then call the file from the PowerShell command line.  Alternatively, copy the following lines and then paste into the command line.

# Microsoft PowerShell Compare-Object Example
# Author: Guy Thomas
Clear-Host
$strReference = Get-Content "C:\Temp\ShopList.txt"
$strDifference = Get-Content "C:\Temp\Checkout.txt"
Compare-Object $strReference $strDifference

Note 4: You could add the explicit parameters: -referenceObject -differenceObject and thus substitute this for the last line of Example 1:
Compare-Object -referenceObject $strReference -differenceObject $strDifference

The reason that I omit these positional parameters is that PowerShell can deduce what to compare from the sequence.

Note 5: If your script fails, then check a) The names of the two files, b) Are they in the folder referenced by your script?

Expected result

InputObject  SideIndicator
————————-
Ice-Cream        =>
Cucumbers      <=
Burgers           <=

Challenge 1: You could add the -IncludeEqual parameter and thus display all the items that you bought that were on the original list.

SideIndicator

=> Means the InputObject is present in the difference (second) file, but not in the first file.  In this scenario it means we bought stuff, which was not on the original list.

<= Present in reference (first) file, but not in the second file.  In my analogy, we forgot to buy this item on our shopping list.

== Present in both files.  Try my challenge and add the -IncludeEqual parameter, then you will see this double == equals sign underneath ‘SideIndicator’ in the output.

# Microsoft PowerShell Diff Example
# Author: Guy Thomas
Clear-Host
$strReference = Get-Content "C:\Temp\ShopList.txt"
$strDifference = Get-Content "C:\Temp\Checkout.txt"
Diff $strReference $strDifference -IncludeEqual |
Format-Table InputObject, SideIndicator -AutoSize

Note 6: I added a Format-Table command so that I could stop the output spreading to the right.  Incidentally, I substituted the alias Diff for Compare-Object.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Example 2: To Compare Two Files, with -Parameters

# Microsoft PowerShell script to research Compare-Object -parameters
# Author: Guy Thomas
Clear-Host
$strReference = Get-Content "C:\Temp\ShopList.txt"
$strDifference = Get-Content "C:\Temp\checkout.txt"
Compare-Object -referenceObject $strReference -differenceObject $strDifference `
-syncWindow 100 -IncludeEqual

Note 7: Observe how Compare-Object relies on Get-Content to provide the stream of data.

Note 8: Even though it’s not necessary with such a short list, I have included my favourite switch – syncWindow because this is how to find extra differences.  The default for -syncWindow is 5.

Because the resulting command would not fit on one line, added the tiny backtick ` to tell PowerShell that the command wrapped onto the next line.

Note 9: You can use the PowerShell alias diff instead of Compare-Object.

See more about PowerShell Parameters ยป

Example 3: How to Research Windows Services

The purpose of this example is to give you ideas for using Compare-Object on a production server.

One classic task for Compare-Object is to investigate Windows services, for example, generate a list of services that have stopped.  One complication is that firstly we need to master Get-WmiObject win32_service.  The second complication is that you need to have objectives and data; for example, a list services that were running yesterday, but have now stopped.  A solution would be to take regular snap-shots of which services are running, save the data into a file, then use Compare-Object to highlight differences, i.e. services that are no longer running.

This mini-project illustrates two factors about scripting in general and PowerShell in particular.  Firstly build-up your scripts gradually, secondly, there are always at least three ways of achieving any scripting goal, in this instance, Get-WmiObject win32_service may have enough properties and methods for you not to need Compare-Object at all.

# Microsoft PowerShell script to list services
# Author: Guy Thomas
Clear-Host
$strState = "running"
$strPath = "C:\Temp\serviceslist2.csv"
Get-WmiObject win32_service -filter "state = ‘$strState’ " |
Select-Object name, caption, status, startmode
Export-Csv -Path $strPath

Note 10: Examine the file referenced by $strPath, Excel would be an ideal program to open this csv file.

Note 11: Research deeper with Get-Member: for example:
Get-WmiObject win32_service | Get-Member

A Big and Advanced Challenge:  I really have left a big task for you apply what you have learned in Example 1, and apply the technique to Windows services.  If you accept this challenge you need to create a difference file, then stop one or more services, finally, run Example 1, but with a modified $strReference and $strDifference.

Engineer's Toolset v10Guy Recommends: SolarWinds Engineer’s Toolset v10

This Engineer’s Toolset v10 provides a comprehensive console of 50 utilities for troubleshooting computer problems.  Guy says it helps me monitor what’s occurring on the network, and each tool teaches me more about how the underlying system operates.

There are so many good gadgets; it’s like having free rein of a sweetshop.  Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools.  Try the SolarWinds Engineer’s Toolset now!

Download your fully functional trial copy of the Engineer’s Toolset v10

Compare-Object Project

Scenario: I had a folder of files.  Many of those files were missing a phrase, I wanted a list of files with these deficiencies.  Stage 1 was to create a text file containing a directory listing.  Stage 2 was to create another file listing only those files containing a particular phrase.  Stage 3 was to employ Compare-Object, process the two files, and then create a third file with a list of all the files that were missing my particular phrase.

Compare-Object – PowerShell Diff Alias

Compare-Object has two aliases, Diff and Compare.

# PowerShell Diff Alias

Get-Alias -definition Compare-Object

# Get-Alias
# Get-Help Get-Alias -full

Note 12: Expected result
Alias Diff
Alias Compare
See more aliases in PowerShell v3.

Mission to Generate a List of Differences

Scenario: I had a folder of files.  Many of those files were missing a phrase, I wanted a list of all files with these deficiencies. 

Stage 1: Obtain a text file containing a directory listing.

Stage 2: Create another file listing only those files containing a particular phrase. 

Stage 3: Employ Compare-Object, process the two files, and then create a third file with a list of all the files that were missing my particular phrase.

Summary of Compare-Object

If you have a scenario where you want to automate processing of two files, and come up with a list of differences; then research Compare-Object and its parameters.  Incidentally, you could use the PowerShell Alias ‘Diff’.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell file tutorials:

PowerShell Home   • Add-Content   • Get-Content   • Set-Content  • PowerShell -Filter   • Test-Path

PowerShell Get-ChildItem   • Get-ChildItem -Include   • Get-ChildItem -Exclude   • Compare-Object

PowerShell Registry  • Get-Credential  • PowerShell ItemProperty  • PowerShell ItemPropery GCI

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.