Windows PowerShell


 

PowerShell Script Files - Compare-Object

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.

Topics for Compare-Object

Mission to Generate a List of Differences

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.

# Microsoft PowerShell script to research compare-Object
# Author: Guy Thomas
# Version 1.2 May 2008 tested on PowerShell v 1.0

help compare-Object -full

Note 1:  With compare-Object, 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.

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 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

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 script to test compare-Object
# Author: Guy Thomas
# Version 1.2 May 2008 tested on PowerShell v 1.0

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.

Expected result

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

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 underneath 'SideIndicator' in the output.

Example 2: To Compare Two Files, with -Parameters

# Microsoft PowerShell script to research compare-Object -parameters
# Author: Guy Thomas
# Version 1.3 May 2008 tested on PowerShell v 1.0

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.

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
# Version 1.4 May 2008 tested on PowerShell v 1.0

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.

  ˚

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.

See more Microsoft PowerShell syntax

PowerShell Home  • Syntax  • -f format  • Pipeline  • Quotes  • Format-table  • Group  • Select-String

Please write in if you see errors of any kind.  Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Google

Webcomputerperformance.co.uk

Guy Recommends: SolarWinds Exchange Monitor

Exchange Monitor from SolarWindsHere is a free tool to monitor your Exchange Server

 

Home Copyright © 1999-2008 Computer Performance LTD All rights reserved

Please report a broken link, or an error.