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

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

Did you try to search for it? :smirk:

Please read the help completely including the examples to learn how to use it.

Compare-Object works for a different usecase, like differential comparison, but in my usecase, the requirement was basically to trigger an action if, name of $a matches $b, so Compare-Object does not actually do the job for me in this use-case.

I posted the same question here: Comparing 2 variables that has got set of arrays as it's values - Microsoft Q&A

And i utilised this logic for my use-case, it simply works.

You wrote …

And for this task you can use Compare-Object. With the parameters -IncludeEqual, -ExcludeDifferent and -PassThru you can get the list of Elements contained in both input arrays. :man_shrugging:t4:

Okay, now i get it, maybe i need to think of how can i process the outputs i receive using this cmdlet, for ex: compare-object -referenceObject $a -differenceObject $b -includeEqual ?

Your description is very abstract. You may make it more specific. Can you share some sample input data (sanitized from sensitive infromation and formatted as code)?

Ok, here it is,
$a=
USER_ID EMP_ID LEVEL


user1 50 IN1
user2 51 IN2
user3. 52 IN3

$b=
User_ID

user1
user10
user3

So, all i want to do is, get the level of user whose user_id from $b matches user_id in $a.

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

$a = @'
USER_ID,EMP_ID,LEVEL
user1,50,IN1
user2,51,IN2
user3,52,IN3
'@ |
    ConvertFrom-Csv

$b = @'
User_ID
user1
user10
user3
'@ |
    ConvertFrom-Csv

Compare-Object -ReferenceObject $a -DifferenceObject $b -Property USER_ID -IncludeEqual -ExcludeDifferent -PassThru

And the output looks like this:

USER_ID EMP_ID LEVEL SideIndicator
------- ------ ----- -------------
user1   50     IN1   ==
user3   52     IN3   ==
1 Like

And then maybe i can pass the output onto the variable and then use it for further actioning?
Ex:

$matches = Compare-Object -ReferenceObject $a -DifferenceObject $b -Property USER_ID -IncludeEqual -ExcludeDifferent -PassThru
foreach($match in $matches){Write-Host " Identical userid "$match.USER_ID"has the level of "$match.LEVEL
}

$Matches is a built in variable of PowerShell and should not be used by you for something else.

Just out of curiousity … what’s the advantage of your output over the plain table? If you don’t want the EMP_ID and the SideIndicator you use a Select-Object

$a = @'
USER_ID,EMP_ID,LEVEL
user1,50,IN1
user2,51,IN2
user3,52,IN3
'@ |
ConvertFrom-Csv

$b = @'
User_ID
user1
user10
user3
'@ |
ConvertFrom-Csv

$CompareObjectSplat = @{
    ReferenceObject  = $a
    DifferenceObject = $b
    Property         = 'USER_ID'
    IncludeEqual     = $true
    ExcludeDifferent = $true
    PassThru         = $true
}
$CompareResultList =
Compare-Object @CompareObjectSplat

$CompareResultList |
    Select-Object -Property 'USER_ID', 'LEVEL'
1 Like

That was to identify userids of one level and then move it to respective OUs, the values of $a was being obtained from an API call to an application.

No. Was I meant was - why do you need a loop adding prosa to the output? That’s just unnecessary in my opinion.

So, the need here is to move only the matching userids of $b in $a to a different OU based on their LEVEL, so i am running an if else condition after the compare-object cmdlet, like if($a.level -like “IN1”){MOve user}

Ah … I see … you don’t need a loop though.

$CompareResultList |
    Where-Object -Property 'LEVEL' -EQ -Value 'IN3'

… for example … :man_shrugging:t4:

1 Like

Yes, that greatly works, thanks for all the guidance and efforts, i really love this forum!!