Comparing 2 variables that has got set of arrays as it's values

Niranjan Pattanashetty 21 Reputation points
2023-01-10T17:40:31.803+00:00

Hi All, I have 2 variables $a and $b, both are set of arrays, but $a has values of 3 column data let’s say name, id, salary, and $b just 1 array of data. Now, I want to run a command to find out whether $a contains $b and if it contains i want to know the ID of the matching Name, how is that possible?

Thanks, Niru

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
4,622 questions
No comments
{count} votes

Accepted answer
  1. Rich Matheisen 35,036 Reputation points
    2023-01-10T19:35:20.78+00:00

    Like this:

    $a = @("A",1,123),@("C",3,175),@("D",4,136) # name, id, salary
    $b = "A","D"                                # name
    ForEach ($employee in $a){
        if ($b -contains $employee[0]){
            Write-Host "Found $($employee[0]) with ID $($employee[1])"
        }
    }
    

0 additional answers

Sort by: Most helpful