When working with arrays in PowerShell, a common task is to compare them to find out which elements are missing from one array when compared to another. PowerShell provides several ways to compare two arrays for missing elements in PowerShell with examples.
To compare two arrays for missing elements in PowerShell, you can use the Compare-Object cmdlet with the -ReferenceObject and -DifferenceObject parameters to identify differences. For example, $missing = Compare-Object -ReferenceObject $array1 -DifferenceObject $array2 -PassThru will output elements present in $array1 but not in $array2. This method is efficient and straightforward for finding missing elements between arrays.
Compare Two Arrays for Missing Elements in PowerShell
Now, let us check various methods to compare two arrays for missing elements in PowerShell.
1. Using Compare-Object Cmdlet
The most straightforward way to compare two arrays in PowerShell is by using the Compare-Object cmdlet. This cmdlet takes two sets of objects and compares them.
Here is a complete PowerShell script that will check for missing elements in a PowerShell array.
$array1 = @(1,2,3,4,5)
$array2 = @(3,4,5,6,7)
$missing = Compare-Object -ReferenceObject $array1 -DifferenceObject $array2 -PassThru
Write-Host "The following elements are missing in the second array:" $missingIn the example above, $array1 is the reference object, and $array2 is the difference object. The -PassThru parameter is used to output the actual missing elements. The result will show you which elements are in $array1 but not in $array2.
You can also see in the screenshot below after I executed the code using VS Code:

2. By Looping Through Arrays
Another method to find missing elements is by manually looping through each array and comparing elements in PowerShell. This method gives you more control over the comparison process.
Here is a complete example of “PowerShell compare two arrays for missing elements“.
$array1 = @(1,2,3,4,5)
$array2 = @(3,4,5,6,7)
$missing = @()
foreach ($item in $array1) {
if ($array2 -notcontains $item) {
$missing += $item
}
}
Write-Host "The following elements are missing in the second array:" $missingHere, we initialize an empty array $missing to store the missing elements. We then loop through each element in $array1 and check if $array2 does not contain it. If it doesn’t, we add it to the $missing array. After the loop, we print out the missing elements.
Once you execute the code, you can see the output in the screenshot below:

3. Using Custom Functions
You might want to create a custom function for more complex comparisons or repeated tasks in PowerShell.
Here is a complete PowerShell script.
function Get-MissingElements ($array1, $array2) {
Compare-Object -ReferenceObject $array1 -DifferenceObject $array2 -PassThru | Where-Object { $_.SideIndicator -eq '<=' }
}
$array1 = @(1,2,3,4,5)
$array2 = @(3,4,5,6,7)
$missing = Get-MissingElements -array1 $array1 -array2 $array2
Write-Host "The following elements are missing in the second array:" $missingIn this example, we define a function Get-MissingElements that uses Compare-Object internally. The function filters out the elements that are only in the reference object (indicated by <=). We then call this function with our arrays and output the missing elements.
4. Using Hashtable for Large Arrays
When dealing with large arrays, performance can become an issue. Hashtables can be used to improve the efficiency of the search operation.
Here is an example of how to use hashtable to compare two arrays for missing elements in PowerShell.
$array1 = @(1,2,3,4,5)
$array2 = @(3,4,5,6,7)
$hashtable = @{}
foreach ($item in $array2) {
$hashtable[$item] = $true
}
$missing = @()
foreach ($item in $array1) {
if (-not $hashtable.ContainsKey($item)) {
$missing += $item
}
}
Write-Host "The following elements are missing in the second array:" $missingIn this example, we first create a hashtable from $array2, setting each element as a key with a value of $true. We then loop through $array1 and check if the hashtable contains the key. If not, the element is missing, and we add it to the $missing array.
You can check the screenshot below for the output, I executed the code using VS code.

Conclusion
Comparing arrays to find missing elements in PowerShell can be done in several ways. The Compare-Object cmdlet is the most direct method and works well for most cases. Looping provides more control and customization, while custom functions are great for reusability. For large arrays, hashtables can offer performance benefits.
I hope now you have a clear idea of how to compare two arrays for missing elements in PowerShell.
You may also like:
- Array Comparisons in PowerShell
- Create an Array Of Objects In PowerShell
- PowerShell Array Parameters
- PowerShell Multidimensional Arrays
- Compare Two Arrays for Matches in PowerShell
- How to Pick Random Items from an Array in PowerShell?
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.