Learning How to Compare Objects with PowerShell

PowerShell can be a little confusing to get started with at first, but just like any other language it’s worth learning. Beginners should learn how to compare objects and PowerShell commands so they understand exactly what is happening on the command line.

The “powershell compare-object examples” is a tutorial that will teach you how to compare objects with PowerShell.

Indeed, comparing files or file contents in Windows is something you’ve done over the years. But doesn’t looking at files and information become tiring after a while? Fortunately, PowerShell’s Compare-Object command allows you to compare objects in any manner you wish.

You’ll learn how to compare items, such as files or file contents, while customizing the result in this article.

Save your eyes the strain and compare items more efficiently!

Understanding PowerShell Objects (Related)

Prerequisites

This will be a hands-on demonstration, but you’ll be OK as long as you have a Windows PC with PowerShell installed. In this lesson, PowerShell version 5.1 is utilized.

Compare-Object in PowerShell for File Contents Comparison

Assume you have two separate files with the identical information. How would you know whether the content of both files is identical? The option might be to use the Get-Content command. The Get-Content command obtains a file’s content from a given location.

Get-Content in PowerShell is a PowerShell Tail Equivalent

To obtain the content (Get-Content) of the requested files into variables ($file1 and $file2), open PowerShell and perform the instructions below. Replace the path below with the path to the files you’d want to compare.

# Save the contents of the text files as variables. Get-Content C: $file1 Temp\File1.txt Get-Content C: $file2 Temp\File2.txt

Using PowerShell Variables to Save Text File ContentUsing PowerShell Variables to Save Text File Content

Understanding PowerShell Variable Scopes is related.

To compare the contents of $file (-ReferenceObject) with $file2, use the Compare-Object command (-DifferenceObject).

Compare-Object does not display the values in both variables by default. To display the values in the output, use the -IncludeEqual argument as a workaround.

Object-Reference-Compare $file1 -Difference Object $file2 -IncludeEqual Object

The SideIndicator reveals if both files have the same content (per line) from the output below:

SideIndicatorDefinition
==The content of both files ($file1 and $file2) is present.
=>Indicates that the content can be found exclusively in the -DifferenceObject ($file2) file.
=>The content can only be found in the -ReferenceObject ($file1) file.

File Contents ComparisonFile Contents Comparison

You may wish to compare strings that are case sensitive. If this is the case, add the -CaseSensitive argument to the Compare-Object command since it is not case-sensitive by default.

To compare $file1 and $file2, use the commands below, both with and without case-sensitivity.

# File Contents Comparison with the -CaseSensitive parameter Compare-Object $file1 $file2 -CaseSensitive # File Contents Comparison without the -CaseSensitive parameter Compare-Object $file1 $file2

The Compare-Object command does not display powershell in the output without the -CaseSensitive argument, as seen below.

File Contents Comparison with Case SensitivityFile Contents Comparison with Case Sensitivity

Comparing strings also works is similar to File Contents Comparison, where you can either specify the strings or use variables to represent the strings to compare. Bear in mind that the Compare-Object command compares source strings and not characters.

Comparing File Lists from Various Locations

Why not compare real files from various places now that you know how to compare file contents? When you’re managing two distinct servers and keeping the same folder for a service with the same files, comparing lists of files comes in helpful.

This time, you’ll use the Get-ChildItem command to compare the list of files in one folder to the list of files in another folder.

Get-ChildItem: Listing Files, Registry, and Certificates is related.

To acquire the lists of files from the C:TempFolder1 and C:TempFolder2 directories, use the commands below and save the lists in the $Folder1List and $Folder2List variables.

# Save a list of file names as variables. Get-ChildItem C: $Folder1List Temp\Folder1\ # Comparing the lists $Folder2List = Get-ChildItem C:TempFolder2 # Compare-Object -IncludeEqual $Folder1List $Folder2List

You may also use the Get-Childitem command’s -Recurse argument to obtain a list of files in subfolders recursively.

Comparing Files from Various FoldersComparing Files from Various Folders

Comparing Files Based on Their Properties

You’ve already compared files in various places, but you may wish to add a property comparison option. You’ll also compare files by lastaccesstime in this demonstration.

To compare files from various directories ($Folder1 and $Folder2) by lastaccesstime, use the following commands.

# Get a list of files from a set of files. Get-ChildItem C: $Folder1 Recurse TempFolder1 Get-ChildItem C: $Folder2 Recurse TempFolder2 # Compare files based on lasttimeaccess and file names. Compare-Object -Property name,lastaccesstime $Folder1 $Folder2

Using the lastaccesstime parameter, compare files.Using the lastaccesstime parameter, compare files.

Processes and Services Comparison

The Compare-Object command’s capabilities go beyond just comparing texts or file content. You may also compare things like procedures and services.

Maybe you’d want to record services between two servers. If that’s the case, you may save the services as variables and compare them.

To compare services on two distinct servers ($server1 and $server2), use the instructions below.

# Save the list of services as variables jumpbox $server1 = Get-Service -ComputerName Get-Service -ComputerName AD $server2 # on both servers, compare services and arrange them by service name Compare-Object $server1 $server2 -PassThru | select name,status,machinename | sort name

Service Comparison Between Two ServersService Comparison Between Two Servers

If you choose to compare processes instead of services, the commands to run are similar. Processes on the same computer or on two distinct servers may also be compared.

Run the following commands to collect and compare ongoing processes from two computers (Jumpbox and AD servers).

# Get a list of processes from remote servers and save them in PowerShell variables. Jumpbox $server1Process = Get-Process -ComputerName Get-Process -ComputerName AD $server2Process # Compare two servers’ processes and list the process names and server names in the output. Compare-Object $server1Process select name,machinename,sideindicator | $server2Process -PassThru

Process Comparison Between Two ServersProcess Comparison Between Two Servers

Comparing Process Lists to Identify Activities

In certain troubleshooting cases, the Compare-Object cmdlet is also useful. How? You may start your action by capturing current running processes in a variable. The running processes are then captured in a new variable, and the new processes are compared.

Use the commands below to compare two lists of captured processes to identify activity.

# Before beginning your activity, capture processes. $Initial Process = Get-Process # Open notepad (your activity) notepad # Capture processes when your activity is finished $New Process = Get-Process # Initial and New Process comparison Compare-Object $Initial Process pick processname,sideindicator $New Process -PassThru

Detecting New Processes on a Local ComputerDetecting New Processes on a Local Computer

Comparing Users from Various Active Directory Groups

Comparing the list of users using the Compare-Object command is useful if you’re handling the same set of users from multiple AD groups. This allows you to keep a careful eye on whether or not a user is absent from one of the two AD groups.

Use the instructions below to compare users from two separate AD groups (staticvmusers and vdivmusers).

# To perform the Get-ADGroup and Get-ADUser commands, import the Active Directory module. # Store users lists from various AD groups into variables using Import-Module ActiveDirectory. Get-ADGroup staticvmusers -Properties Member |Select-Object -ExpandProperty Member | Get-ADUser $FirstGroup = Get-ADGroup staticvmusers -Properties Member Get-ADGroup vdivmusers -Properties Member |Select-Object -ExpandProperty Member | Get-ADUser # $SecondGroup = Get-ADGroup vdivmusers -Properties Member | Get-ADUs To compare people in the two AD groups, use the compare-object cmdlet. -Property ‘SamAccountName’ -IncludeEqual Compare-Object ($FirstGroup) ($SecondGroup)

Users in Two AD Groups are ComparedUsers in Two AD Groups are Compared

Conclusion

You’ve learned how to compare things using PowerShell in a variety of ways in this article, from file contents to people in AD groups. You’ve recognized that comparing files, services, processes, and other objects is simple using the Compare-Object cmdlet.

You now have a basic understanding of how to use the Compare-Object cmdlet. Why not expand on your newfound knowledge by learning how to compare arrays in PowerShell?

PowerShell has a built-in command called “Compare-Object” that can be used to compare two arrays. This article will show how to use it and provide some examples of what it does. Reference: powershell compare-object array.

  • compare-object powershell csv
  • powershell compare-object show only differences
  • compare-object show differences only
  • compare-object all properties
  • powershell compare-object multiple properties
You May Also Like