How to compare two arraylist and remove duplicate value

HinW 20 Reputation points
2023-02-15T07:57:12.85+00:00

I have two array. I would like to compare array1 to see if there any values that exist in array2 and return a new array with below expected result. Let say the outcome object is called $array3

Thanks

    $array1 = @("1", "2", "3", "4") 
    $array2 = @("5", "4", "6", "7", "8")

expected outcome:
1,2,3
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2023-02-15T08:26:55.1466667+00:00

    Hi @HinW ,

    comparing 2 arrays is possible using the Compare-Object cmdlet:

    $array1 = @("1", "2", "3", "4") 
    $array2 = @("5", "4", "6", "7", "8")
    $array3 = (Compare-Object $array1 $array2 -IncludeEqual | Where-Object { $_.SideIndicator -eq "<=" }).InputObject
    $array3
    

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards

    Andreas Baumgarten

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.