Using the Compare-Object Cmdlet
Comparing Two Objects or Text Files
Compare-Object is a cmdlet that has some intriguing possibilities; after all, it compares two objects and lets you know what differences, if any, exist between the two. For example, suppose you use this command to store information about the processes currently running on a computer in a variable named $A:
$A = Get-Process
Say you now start and stop a few applications and then store the latest set of process information in a variable named $B:
$B = Get-Process
That gives you a pair of objects that contain information about the processes running on the computer, $A representing the processes that were running at, say, 10:00 and $B representing the processes that were running at (to pick a time) 10:30.
Once you have those two objects in hand you can use Compare-Object to determine which processes (if any) were stopped or started during the interim between $A and $B. Here’s the command to run:
Compare-Object $A $B
And here’s the kind of output you’ll get back:
InputObject SideIndicator ----------- ------------- System.Diagnostics.Process (EXCEL) => System.Diagnostics.Process (freecell) => System.Diagnostics.Process (notepad) => System.Diagnostics.Process (dexplore) <=
The => sign indicates that the process in question (e.g., Freecell) was found in the property set of the second object but not found in the property set for the first object. That means Freecell was started sometime after we grabbed the first set of process information (the data stored in $A). The <= sign indicates that Dexplore was found in the dataset for $A but not the dataset for $B. That can mean only that the application was initially running, but then was terminated before we grabbed the second set of process information.
In other words, after we took our first process snapshot we started Excel, Freecell, and Notepad, and terminated Dexplore. See how that works?
Compare-Object can also be used to compare the contents of two text files. Suppose we have two files in the folder C:\Scripts. The file named X.txt contains the following:
This is line number 1. Line 2. This is line number 3.
File Y.txt, in turn, looks like this:
This is line number 1. This is line number 2. This is line number 3.
To determine if there are any differences between the two files we simply use this command:
Compare-Object $(Get-Content c:\scripts\x.txt) $(Get-Content c:\scripts\y.txt)
And here’s what Compare-Object reports back:
InputObject SideIndicator ----------- ------------- This is line number 2. => Line 2. <=
Notice that we use the Get-Content cmdlet to retrieve the contents of the two text files, and that each Get-Content command is enclosed in parentheses; that ensures that we pass objects (as opposed to, say, string values) to Compare-Object. In effect, what we’ve done here is create a single command that encompasses these three individual commands:
$A = Get-Content c:\scripts\x.txt $B = Get-Content c:\scripts\y.txt Compare-Object A$ B$
If you’d like to see a line-byline comparison of the two text files, then add the -includeequal parameter:
Compare-Object $(Get-Content c:\scripts\x.txt) $(Get-Content c:\scripts\y.txt) -includeequal
That results in output similar to this:
InputObject SideIndicator ----------- ------------- This is line number 1. == This is line number 3. == This is line number 2. => Line 2. <=