---
title: "Array Comparisons Made Easy with PowerShell"
description: "Compare arrays like a pro. Learn how to perform complex array comparisons using PowerShell commands."
canonical: "https://adamtheautomator.com/powershell-compare-arrays/"
---

# Array Comparisons Made Easy with PowerShell

> Compare arrays like a pro. Learn how to perform complex array comparisons using PowerShell commands.

Source: https://adamtheautomator.com/powershell-compare-arrays/

---

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

![Array Comparisons Made Easy with PowerShell](https://adamtheautomator.com/wp-content/uploads/2019/06/5d238ae5c824b514689ea57f.jpg)

# Array Comparisons Made Easy with PowerShell

[![](https://secure.gravatar.com/avatar/d0b9d42e21e5622713f8b693aa5c0f9244d5f7dd200ed29b8398f52dee5de337?s=192&d=mm&r=g)Adam Bertram](https://adamtheautomator.com/author/adam-bertram/)29 June 20193 min. read

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

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

Table of Contents

*   [Comparing Arrays of Strings](#comparing-arrays-of-strings)
*   [Using the -Contains or -In Operators](#using-the-contains-or-in-operators)
*   [Using Where-Object](#using-where-object)
*   [Using the Compare-Object Cmdlet](#using-the-compare-object-cmdlet)
*   [Comparings Arrays of Complex Objects](#comparings-arrays-of-complex-objects)
*   [Conclusion](#conclusion)

Using some PowerShell kung-fu, you can easily do Powershell compare arrays of all kinds of objects. There are many different scenarios you may find yourself in so let’s dig in and see the ways we can build PowerShell to compare arrays.

Not a reader? Watch this related video tutorial!

**_Not seeing the video? Make sure your ad blocker is disabled._**

To determine the best way to compare arrays, you must first figure out what types of elements are in both arrays.

*   Do both arrays contains all of the same type of object?
*   Do both arrays have the same number of elements?
*   Are there different types of objects in each array?

You must know the answer to each of these questions before you can accurate compare arrays. Let’s cover each scenario.

## Comparing Arrays of Strings

One of the easiest ways to compare arrays with PowerShell is if you have two arrays only containing strings. When you find yourself in this position, you’ve got a few different ways to compare strings in the arrays.

### Using the -Contains or -In Operators

The `-contains` operator is a PowerShell operator that allows you to check to see if an object is in a collection. The `-contains` operator natively doesn’t understand collections but y0u can build code to make it do your bidding.

Let’s say a collection (array) contains four strings like below.

```powershell
$array = @('blue','red','purple','pink')
```

The `-contains` operator works by checking if a single string is in that array like this:

```powershell
$array -contains 'pink'
```

When the collection on the left _contains_ that string, PowerShell will return True. If not, it will return False.

![PowerShell -contains Operator](https://adamtheautomator.com/content/images/2019/07/image-8.png)

PowerShell -contains Operator

We can compare arrays using the `-contains` operator by reading each string in an array and checking to see if the other array _contains_ that string.

Let’s say I want to compare two arrays to see which strings in the first array exist in the second array.

```powershell
$array = @('blue','red','purple','pink')
$array2 = @('brown','red','black','yellow')

$array | ForEach-Object {
    if ($array2 -contains $_) {
        Write-Host "`$array2 contains the `$array1 string [$_]"
    }
}
```

You could alternatively use the `-in` operator which is identical to the `-contains` operator yet the syntax is opposite. When using the `-contains` operator, the array is defined on the left side. Using the `-in` operator, the array is defined on the right side like below:

```powershell
$array | ForEach-Object {
    if ($_ -in $array2) {
        Write-Host "`$array2 contains the `$array1 string [$_]"
    }
}
```

### Using Where-Object

Alternatively, you could also use the [`Where-Object`](https://adamtheautomator.com/powershell-where-object/ "Where-Object") cmdlet to return all strings in one array in the other like below.

```powershell
$array | Where-Object -FilterScript { $_ -in $array2 }
```

### Using the Compare-Object Cmdlet

You can also use [PowerShell](https://adamtheautomator.com/tag/powershell/) to compare arrays using the `Compare-Object` cmdlet. This cmdlet takes a reference object and a difference object and returns a side indicator indicating which elements are and are not in either array.

```powershell
Compare-Object -ReferenceObject $array -DifferenceObject $array2
```

![Using Compare-Object](https://adamtheautomator.com/content/images/2019/07/image-9.png)

Using Compare-Object

You can see below that the `Compare-Object` cmdlet allows you to compare both arrays at once. If the `SideIndicator` property is `=>`, this means the `InputObject` property returned is in the `DifferenceObject` value and not in the `ReferenceObject` value and vice versa for the `<=` `SideIndicator`.

By default, `Compare-Object` returns differences. You can also return all of the strings in each array that are in both by using the `IncludeEqual` parameter.

![Comparing arrays with Compare-Object](https://adamtheautomator.com/content/images/2019/07/image-10.png)

Comparing arrays with Compare-Object

## Comparings Arrays of Complex Objects

Simple enough, right? Now, let’s bring objects into the mix. Let’s say we’ve got a field in this HR database of ours and we’d like to populate this into the Active Directory description field. Prior to doing this, we first have to have a common identifier. In my environment, there’s an employee number both in the HR database and in the custom Active Directory attribute. So let’s attempt to match this.

First, let’s see what happens if we try our previous approach. Here’s the CSV file we’re using.

![CSV output](https://adamtheautomator.com/content/images/2019/07/powershell-comparing-object-property-with-two-object-arrays---AD-datasoource.png)

CSV output

Here’s how I get our two datasets.

```powershell
$ad_users = Get-AdUser -Filter {enabled -eq $true} -Properties employeeNumber | select employeenumber,samaccountname,description
$users_from_database = Import-Csv 'database_users.csv' | select employee number
```

What happens when we run these two arrays through the same scenario as our strings? Absolutely nothing. Why?

The reason is because you can’t typically say `$object1 -eq $object2` because objects are more complex than a simple string, boolean or integer. There are some other circumstances where this isn’t the case but I try to make it a habit to compare object properties; not entire objects. So in this instance, we have to do something like this:

```powershell
$ad_user[0].employeeNumber -eq $users_from_database[0].employeeNumber
```

What’s the solution? Currently, I’ve got two solutions. When dealing with thousands of objects it’s not quick but it works. I’d like to know if anyone else has any other suggestions.

```powershell
$ad_employee_numbers = $ad_users | % {$_.employeenumber}

## Create an array of strings of only the AD user's employee number
$users_from_database | Where-Object -FilterScript { $ad_employee_numbers -contains $_.employeeNumber }
```

We can also use `Compare-Object` although this is slower.

```powershell
$ad_employee_numbers = $ad_users | ForEach-Object {$_.employeenumber}

## Create an array of strings of only the AD user's employee number
$database_user_employee_numbers = $users_from_database | ForEach-Object {$_.employeenumber}

## Create an array of strings of only the database user's employee number
(Compare-Object $ad_employee_numbers $database_user_employee_numbers -IncludeEqual | Where-Object -FilterScript {$_.SideIndicator -eq '=='}).InputObject
```

## Conclusion

There are many different ways to use PowerShell to compare arrays. Arrays can be complex and thoroughly understanding objects inside of the arrays will greatly help you compare them.

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-compare-arrays%2F&text=Array%20Comparisons%20Made%20Easy%20with%20PowerShell)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-compare-arrays%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-compare-arrays%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/)
