Those aren’t arrays though. They are just a list of strings, maybe the output you were expecting???
Anyway, this is an array…
$myArray = 64,"Hello",3.5,"World"
Or this…
$usa_states=@{ CA="California";
"NY" = "New York";
"IL" = "Illinois";
"NH" = "New Hampshire"
}
Array keys (left side of the equal sign ), in a literal hash, must be unique, and yours are not. Notice you have repeated name, surname and login keys, in these hash literals.
$first = @{'name' = 'Fred';
'surname' = 'White';
'login' = 'fred.white';
'name' = 'Lisa';
'surname' = 'Smith';
'login' = 'lisa.smith'
}
$second = @{'name' = 'Fred';
'surname' = 'White';
'login' = 'fred.white';
'name' = 'Kylie';
'surname' = 'Brown';
'login' = 'kylie.brown'
}
If you paste the above in to the PowerShell ISE or VSCode, they will immediately show errors. Those dreaded red squiggles. Hover over those squiggles, to see the error description.
I get you are trying to do this for multiple users, but… Take a read of these write ups, if you have not already.
Powershell: Everything you wanted to know about arrays
Multidimensional arrays in Powershell
So, taking out the duplicates -
$first = @{'name' = 'Fred';
'surname' = 'White';
'login' = 'fred.white'
}
$second = @{'name' = 'Fred';
'surname' = 'White';
'login' = 'fred.white'
}
Once getting thru those, the thought becomes:
Multi-dimensional array - Basic strings
# Multi-dimensional array
($Marshmallows = @(("Pink","Yellow","Orange","Green","Blue"),("Hearts","Stars","Moons","Clovers","Diamonds")))
# Results
Pink
Yellow
Orange
Green
Blue
Hearts
Stars
Moons
Clovers
Diamonds
$Marshmallows[0][1]
Yellow
$Marshmallows[0][2]
Yellow
$Marshmallows[1][3]
Clovers
$Marshmallows[1][4]
Diamonds
Or properly constructing your Multi-dimensional arrays becomes this…
($first = (@{'name' = 'Fred';'surname' = 'White';'login' = 'fred.white'}),
(@{'name' = 'Lisa';'surname' = 'Smith';'login' = 'lisa.smith'}))
# Resutls
Name Value
---- -----
name Fred
login fred.white
surname White
name Lisa
login lisa.smith
surname Smith
$first[0]
Name Value
---- -----
name Fred
login fred.white
surname White
($second = (@{'name' = 'Fred';'surname' = 'White';'login' = 'fred.white'}),
(@{'name' = 'Kylie';'surname' = 'Brown';'login' = 'kylie.brown'}))
# Results
Name Value
---- -----
name Fred
login fred.white
surname White
name Kylie
login kylie.brown
surname Brown
$second[1]
Name Value
---- -----
name Kylie
login kylie.brown
surname Brown
Putting it together with, it this what you are after.
Clear-Host
$first = (@{
'name' = 'Fred';
'surname' = 'White';
'login' = 'fred.white'}
),
(@{'name' = 'Lisa';
'surname' = 'Smith';
'login' = 'lisa.smith'}
)
$second = (@{
'name' = 'Fred';
'surname' = 'White';
'login' = 'fred.white'}
),
(@{
'name' = 'Kylie';
'surname' = 'Brown';
'login' = 'kylie.brown'}
)
$matched = @()
$unmatched = @()
Foreach ($data1 in $first)
{
Write-Host "Validating $($data1.login) in compared array" -ForegroundColor Yellow
If ($second.login -contains $data1.login)
{
Write-Host "Match found in compared array for $($data1.login)`n" -ForegroundColor Green
}
Else
{ Write-Warning -Message "No match found in compared array for $($data1.login)"}
}
# Results
Validating fred.white in compared array
Match found in compared array for fred.white
Validating lisa.smith in compared array
WARNING: No match found in compared array for lisa.smith