Compare-Object to Find the Differences Between Files
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.
In this mission I had a directory of files. Some of those files were
missing a phrase, I wanted a list of files with these deficiencies. Stage one was to create a
text file containing a directory listing. Stage two was to create another
file listing only those files containing a particular phrase. Stage three
was to launch Windows PowerShell and then call for Compare-Object. Using
this technique you can process the two files and create a third file
with a list of all the files that were missing my particular phrase. In
these ezine I just want to focus on stage three, generating a list of
differences.
Preparation: Use PowerShell's Built-in Help
This script is a useful reminder to see 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, what we learn is that 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 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, there is a PowerShell diff alias.
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 if you pretend that you have a shopping list.
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
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 1: 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 2: If your script fails, then check a) The names of the two files, b) They are in the folder referenced by your script.
Challenge 1: You could add the -includeEqual parameter and
thus display all the items that you bought that were on the original list.
=> 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 underneath 'SideIndicator' in the output.
Guy
Recommends: WMI Monitor and It's Free!
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems. Fortunately, Solarwinds
have created a
Free WMI Monitor so that you can discover these gems of performance
information, and thus improve your PowerShell scripts. Take the guess work out of which WMI counters to use when scripting the
operating system, Active Directory or Exchange Server.
# 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 1: Observe how Compare-Object relies on
Get-Content to provide the stream of data
Note 2: Even though it's not necessary with such a short
list, I have included my favourite switch - syncWindow. 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 3: You can use the PowerShell alias diff
instead of Compare-Object.
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 1: Examine the file referenced by $strPath,
Excel would be an ideal program to open this csv file.
Note 2: Research deeper with Get-Member: for example:
Get-WmiObject win32_service |gm
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.
Guy Recommends: A Free Trial of the Network Performance Monitor
(NPM)
Solarwinds'
Orion 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.
Perhaps the NPM's best feature is the way it suggests solutions to network
problems. Its
second best feature is 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 take advantage of Solarwinds' offer.
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
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.
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems.
Fortunately, Solarwinds
have created the
Free WMI Monitor so that you can actually see and understand these gems of
performance information. Take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.