---
title: "Compare Objects with PowerShell (Step by Step Guide)"
description: "Learn how to compare objects with PowerShell’s Compare-Object cmdlet in this tutorial that will come in handy for your admin tasks!"
canonical: "https://adamtheautomator.com/compare-objects-with-powershell/"
---

# Compare Objects with PowerShell (Step by Step Guide)

> Learn how to compare objects with PowerShell’s Compare-Object cmdlet in this tutorial that will come in handy for your admin tasks!

Source: https://adamtheautomator.com/compare-objects-with-powershell/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Compare Objects with PowerShell (Step by Step Guide)](https://adamtheautomator.com/wp-content/uploads/2022/03/Learning-How-to-Compare-Objects-with-PowerShell.jpg)

# Compare Objects with PowerShell (Step by Step Guide)

[![](https://secure.gravatar.com/avatar/c14b031fe4a12b0e3f4d1e112e37261df83921ddc370cdafbad2a614e36f0cf4?s=192&d=mm&r=g)Chaitanya](https://adamtheautomator.com/author/chaitanya-g/)10 March 20225 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[PowerShell](/tag/powershell/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Comparing File Contents](#comparing-file-contents)
*   [Comparing Lists of Files from Different Locations](#comparing-lists-of-files-from-different-locations)
*   [Compare Objects with PowerShell by Properties](#compare-objects-with-powershell-by-properties)
*   [Comparing Processes and Services](#comparing-processes-and-services)
*   [Identifying Activities by Comparing Lists of Processes](#identifying-activities-by-comparing-lists-of-processes)
*   [Comparing Users Between Different Active Directory Groups](#comparing-users-between-different-active-directory-groups)
*   [Conclusion](#conclusion)

Sure enough, comparing files or file contents in Windows is one thing you’ve been doing through the years. But doesn’t eyeballing files and contents strain your eyes at some point? Well, lucky for you, you can compare objects with PowerShell the way you want to with the [`Compare-Object`](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/compare-object?view=powershell-7.2) command.

In this tutorial, you’ll learn to compare objects with PowerShell, like files or file contents, while customizing the output.

Save your eyes the strain and effectively compare objects!

Related:[Understanding PowerShell Objects](https://adamtheautomator.com/powershell-objects/)

## Prerequisites

This tutorial will be a hands-on demonstration, but so long as you have a Windows PC with PowerShell, you’re good to go. PowerShell version 5.1 is used in this tutorial.

## Comparing File Contents

Imagine you’re keeping two different files with the same content. How would you know if both files keep the same content? Running the `Get-Content` command can be the solution. The `Get-Content` command retrieves the content of a file from a specified location.

Related:[PowerShell Get-Content a PowerShell Tail Equivalent](https://adamtheautomator.com/powershell-get-content/)

Open PowerShell, and run the below commands to get the content (`Get-Content`) of the specified files into variables (`$file1` and `$file2`). Replace the path below with your chosen files’ path to compare.

```powershell
# Grab the content of the text files into variables
$file1 = Get-Content C:\\Temp\\File1.txt
$file2 = Get-Content C:\\Temp\\File2.txt
```

![Storing Text File Contents to PowerShell Variables](https://adamtheautomator.com/wp-content/uploads/2022/03/image-116.png)

Storing Text File Contents to PowerShell Variables

Related:[Understanding PowerShell Variable Scopes](https://adamtheautomator.com/powershell-scopes/)

Now, run the `Compare-Object` command below to compare contents of `$file` (`-ReferenceObject`) with `$file2` (`-DifferenceObject`).

By default, `Compare-Object` does not show the values present in both variables. As a solution, add the `-IncludeEqual` parameter to show the values in the output.

```powershell
Compare-Object -ReferenceObject $file1 -DifferenceObject $file2 -IncludeEqual
```

From the output below, the **SideIndicator** tells whether both files have the same content (per line) or not:

<table><tbody><tr><td>SideIndicator</td><td>Definition</td></tr><tr><td>==</td><td>Indicates the content is present in both files ($file1 and $file2).</td></tr><tr><td>=&gt;</td><td>Indicates the content only exists in the -DifferenceObject file ($file2).</td></tr><tr><td>=&gt;</td><td>Indicates the content only exists in the -ReferenceObject file ($file1).</td></tr></tbody></table>

![Comparing File Contents](https://adamtheautomator.com/wp-content/uploads/2022/03/image-117.png)

Comparing File Contents

Perhaps you prefer to compare strings with case sensitivity. If so, append the `-CaseSensitive` parameter since the `Compare-Object` command is not case-sensitive by default.

Run the below commands to compare `$file1` with `$file2`, both with and without case-sensitivity.

```powershell
# Comparing file contents with the -CaseSensitive parameter
Compare-Object $file1 $file2 -CaseSensitive
# Comparing file contents without the -CaseSensitive parameter
Compare-Object $file1 $file2
```

As you can see below, without the `-CaseSensitive` parameter, the `Compare-Object` command doesn’t show **powershell** in the output.

![Compare Objects with PowerShell : Comparing File Contents with Case Sensitivity](https://adamtheautomator.com/wp-content/uploads/2022/03/image-118.png)

Compare Objects wth PowerShell : Comparing File Contents with Case Sensitivity

> _Comparing strings also works is similar to comparing file contents, 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 Lists of Files from Different Locations

Now that you know how to compare file contents, why not compare actual files from different locations? Comparing lists of files comes in handy when you’re managing two different servers where you keep the same folder for a service containing the same files.

This time, you’ll use the `Get-ChildItem` command to get the list of files in a folder to compare with another folder’s list of files.

Related:[Get-ChildItem: Listing Files, Registry and Certificates](https://adamtheautomator.com/get-childitem/)

Run the below commands to get the lists of files from C:\\Temp\\Folder1\\ and C:\\Temp\\Folder2 folders and store the lists in the $Folder1List and $Folder2List variables.

```powershell
# Capture the list of file names into variables
$Folder1List = Get-ChildItem C:\\Temp\\Folder1\\
$Folder2List = Get-ChildItem C:\\Temp\\Folder2\\
# Comparing the lists
Compare-Object $Folder1List $Folder2List -IncludeEqual
```

> _You can also use `-Recurse` parameter in the `Get-Childitem` command to recursively retrieve a list of files in subfolders._

![Comparing Files in Different Folders](https://adamtheautomator.com/wp-content/uploads/2022/03/image-119.png)

Comparing Files in Different Folders

## Compare Objects with PowerShell by Properties

You’ve just compared files in different locations, but you may want to add another option to compare files by properties. And in this demo, you’ll compare files by lastaccesstime.

Run the following commands to compare files from different folders ($Folder1 and $Folder2) by lastaccesstime.

```bash
# Retrieve list of files from specified files
$Folder1 = Get-ChildItem C:\\Temp\\Folder1\\ -Recurse
$Folder2 = Get-ChildItem C:\\Temp\\Folder2\\ -Recurse
# Compare files by lasttimeaccess with file names
Compare-Object $Folder1 $Folder2 -Property name,lastaccesstime
```

![Listing Compared Files using the lastaccesstime Parameter](https://adamtheautomator.com/wp-content/uploads/2022/03/image-120.png)

Listing Compared Files using the lastaccesstime Parameter

## Comparing Processes and Services

The `Compare-Object` command’s capabilities are not limited to just comparing strings or content in files. You can also compare processes, services, and so on.

Perhaps you want to capture services between two servers. If so, you can store the services into variables and compare the services.

Run the following commands to compare services between two different servers (`$server1` and `$server2`).

```powershell
# capture the services list into variables
$server1 = Get-Service -ComputerName jumpbox
$server2 = Get-Service -ComputerName AD
# compare services on both the servers and sort them as per the service name
Compare-Object $server1 $server2 -PassThru | sort name| select name,status,machinename
```

![Comparing Services Between Two Servers](https://adamtheautomator.com/wp-content/uploads/2022/03/image-121.png)

Comparing Services Between Two Servers

If you prefer to compare processes, the commands to execute are similar to comparing services. You can also compare processes on the same machine or two different servers.

Run the below commands to capture running processes from two machines (`Jumpbox` and `AD` servers) into variables and compare them.

```powershell
# Get process list from remote servers into PowerShell variables
$server1Process = Get-Process -ComputerName Jumpbox
$server2Process = Get-Process -ComputerName AD
# Compare processes from two servers and list process names and the corresponding server name in the output
Compare-Object $server1Process $server2Process -PassThru | sort name  |select name,machinename,sideindicator
```

![Comparing Processes Between Two Servers](https://adamtheautomator.com/wp-content/uploads/2022/03/image-122.png)

Comparing Processes Between Two Servers

## Identifying Activities by Comparing Lists of Processes

The `Compare-Object` cmdlet is also helpful in some troubleshooting scenarios. How? You can capture current running processes into a variable and start your activity. You then capture the running processes into a new variable and compare the new processes.

Run the below commands to identify activities by comparing two different lists of captured processes.

```powershell
# Capture processes before starting your activity.
$Initial_Process = Get-Process
# Open notepad (your activity)
notepad
# Capture processes after your activity completes.
$New_Process = Get-Process
# Comparing Initial and New Process
Compare-Object $Initial_Process $New_Process -PassThru | select processname,sideindicator
```

![Identifying New Processes on Local PC](https://adamtheautomator.com/wp-content/uploads/2022/03/image-123.png)

Identifying New Processes on Local PC

## Comparing Users Between Different Active Directory Groups

If you’re managing the same set of users from different AD groups, comparing the list of users with the `Compare-Object` command will come in handy. Doing so lets you keep a close watch if a user is missing from either of the two different AD groups.

Run the below commands to compare users between two different AD groups (`staticvmusers` and `vdivmusers`).

```powershell
# Import Active directory module to run Get-ADGroup and Get-ADUser commands.
Import-Module ActiveDirectory
# Store users list from different AD groups into variables.
$FirstGroup = Get-ADGroup staticvmusers -Properties Member |Select-Object -ExpandProperty Member | Get-ADUser
$SecondGroup = Get-ADGroup vdivmusers -Properties Member |Select-Object -ExpandProperty Member | Get-ADUser
#Run compare-object cmdlet to compare users in those two AD groups.
Compare-Object ($FirstGroup) ($SecondGroup) -Property 'SamAccountName' -IncludeEqual
```

![Comparing Users in Two AD Groups](https://adamtheautomator.com/wp-content/uploads/2022/03/image-124.png)

Comparing Users in Two AD Groups

## Conclusion

In this tutorial, you’ve learned dabs of how to compare objects with PowerShell in many ways, from file contents to users in AD groups. You’ve realized that the `Compare-Object` cmdlet is a quick solution to comparing files, services, processes, and so on.

You’ve gained the basic knowledge of using the `Compare-Object` cmdlet. Now, why not build on this newfound knowledge by [learning powerful PowerShell compare arrays](https://adamtheautomator.com/powershell-compare-arrays/)?

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fcompare-objects-with-powershell%2F&text=Compare%20Objects%20with%20PowerShell%20\(Step%20by%20Step%20Guide\))[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fcompare-objects-with-powershell%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fcompare-objects-with-powershell%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2026/06/26995-troubleshoot-dns-issues-powershell-codex.webp)

### [Troubleshoot DNS Issues with PowerShell](/troubleshoot-dns-issues-powershell/)

Troubleshoot DNS issues with PowerShell by testing name resolution, DNS client settings, cache entries, and network connectivity in a repeatable workflow.

![](https://adamtheautomator.com/wp-content/uploads/2025/10/image_2025-10-24_095322075.png)

### [Migrating from PowerShell 6 to 7.5: Breaking Changes/New Features](/migrating-powershell-6-to-7-5/)

Migrate from PowerShell Core 6 to 7.5: breaking changes, features, and testing tips.

![](https://adamtheautomator.com/wp-content/uploads/2025/06/featured-image-6.png)

### [How to Add Timeouts to Pester Tests with PowerShell Runspaces](/pester-test-timeout-runspaces/)

Prevent Pester tests from hanging indefinitely using PowerShell runspaces. Learn to handle variable scoping, module loading, TestDrive access, and stream capture challenges with timeout protection.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
