I have two arrays in Powershell. Each Array contains an array of objects. These objects have two properties:
Name: String
Id: GUID
The first Array has 4413 objects in it and the second has 4405. The counts are irrelevant, but I only mention them to note that the contents of Array1 and Array2 are different.
Here is my current code (pseudo):
#Fill Array1
$Array1 = Fill-Array1
#Fill Array2
$Array2 = Fill-Array2
#Loop through the arrays and write out the names of all items in Array2 that are different than Array1
ForEach($Val in $Array2)
{
$Name = $Val.Name
If($Array1 -notcontains $Val) //this does not work
{
Write-Host $Name
}
}
What is the correct way to check for the existence of the object in Array1? Is my only option to do a nested loop?
Update, using the answer from Manu P below, the following is how I implemented the solution:
#Fill Array1
$Array1 = Fill-Array1
#Fill Array2
$Array2 = Fill-Array2
#Compare the arrays
$ComparedResults = Compare-Object -ReferenceObject $Array1 -DifferenceObject $Array2 #I left out the -IncludeEqual param because I don't care about those results
ForEach($Result in $ComparedResults)
{
If($Result.SideIndicator -eq "=>") #the value in Array2 is different than Array1
{
$Val = $Result.InputObject
Write-Host $Val.Name
}
}