Comparison of two arrays

Good morning everyone.
I kindly need some help on this
the comparison of two arrays.

The scenario is this:

I have 2 CSV files with this structure

CSV n.1
Name, Surname, Login, State
CSV n.2
Name, Surname, Login, State

The code should do this:
For each entry.Login of the CSV file n.1 must
search for the corresponding entry.Login of the CSV file n.2

If it matches, the State field must be checked.

I try to explain it with an example
CSV n.1
Name, Surname, Login, State
Karl, Malone, KarlM, Active
Larry, Bird, LarryB, Active
Dennis, Rodman, DennisR, Disabled

CSV n.2
Name, Surname, Login, State
Dennis, Rodman, DennisR, Active
Karl, Malone, KarlM, Active

Output
Karl, Malone, KarlM -> OK (because Active in each file)
Larry, Bird, LarryB -> ERROR (because is missing in the second file)
Dennis, Rodman, DennisR -> ERROR (because Disabled in first file but Enabled in the second file)

Could someone give me some help on how to do it?

 

Thank you very much

Best regards

Andrea

Use Compare-Object:

$CSV1 = @"
Name, Surname, Login, State
Karl, Malone, KarlM, Active
Larry, Bird, LarryB, Active
Dennis, Rodman, DennisR, Disabled
"@ | ConvertFrom-Csv

$CSV2 = @"
Name, Surname, Login, State
Dennis, Rodman, DennisR, Active
Karl, Malone, KarlM, Active
"@ | ConvertFrom-Csv

Compare-Object -ReferenceObject $CSV1 -DifferenceObject $CSV2 -Property Name,State -PassThru

Output:

Name          : Dennis
Surname       : Rodman
Login         : DennisR
State         : Active
SideIndicator : =>

Name          : Larry
Surname       : Bird
Login         : LarryB
State         : Active
SideIndicator : <=

Name          : Dennis
Surname       : Rodman
Login         : DennisR
State         : Disabled
SideIndicator : <=
# Compare records 

#region Sample Data
@'
Name, Surname, Login, State
Karl, Malone, KarlM, Active
Larry, Bird, LarryB, Active
Dennis, Rodman, DennisR, Disabled
'@ | Out-File .\n1.csv

@'
Name, Surname, Login, State
Dennis, Rodman, DennisR, Active
Karl, Malone, KarlM, Active
'@ | Out-File .\n2.csv

#endregion

#region Read and prepare data
$Data1 = Import-Csv .\n1.csv
$Data2 = Import-Csv .\n2.csv

# To match records based on concatenation of name + surname + login fields, add 'Id' field:
$Data1 | Add-Member -MemberType NoteProperty -Name Id -Value $null -EA 0 
$Data2 | Add-Member -MemberType NoteProperty -Name Id -Value $null -EA 0 
$Data1 | foreach { $_.Id = "$($_.Name)$($_.Surname)$($_.Login)" }
$Data2 | foreach { $_.Id = "$($_.Name)$($_.Surname)$($_.Login)" }

#endregion

#region Process
$myOutput = foreach ($Record in $Data1) {
    if ($Found = $Data2 | where Id -EQ $Record.Id) {
        if ($Record.State -eq $Found.State) {
            New-Object -TypeName PSObject -Property ([Ordered]@{
                Name    = $Record.Name
                Surname = $Record.Surname
                Login   = $Record.Login
                State   = $Record.State
                Match   = $true
                Reason  = 'Identical State in both files'
            })
        } else {
            New-Object -TypeName PSObject -Property ([Ordered]@{
                Name    = $Record.Name
                Surname = $Record.Surname
                Login   = $Record.Login
                State   = $Record.State
                Match   = $false
                Reason  = "n1 file State is '$($Record.State)' while n2 file State is '$($Found.State)'"
            })
        }
    } else {
        New-Object -TypeName PSObject -Property ([Ordered]@{
            Name    = $Record.Name
            Surname = $Record.Surname
            Login   = $Record.Login
            State   = $Record.State
            Match   = $false
            Reason  = 'Exists in n1 file but not in n2 file'
        })
    }
}
#endregion

# Output
$myOutput | FT -a 
Name   Surname Login   State    Match Reason                                                     
----   ------- -----   -----    ----- ------                                                     
Karl   Malone  KarlM   Active    True Identical State in both files                              
Larry  Bird    LarryB  Active   False Exists in n1 file but not in n2 file                       
Dennis Rodman  DennisR Disabled False n1 file State is 'Disabled' while n2 file State is 'Active'

Sam, you are a monster !!!
In a good way of course :slight_smile:

Your code is superlative.
Unfortunately I understand little.
I am definitely ignorant.

Sam, I don’t know how to thank you.

Rob, many thanks to you too for the invaluable support.

Thanks to both of you for wasting time helping a complete stranger!

Good day

Andrea