Sree
Sree

Reputation: 983

Compare two lists in Powershell

I have just started working on powershell. I have two lists of 132 and 134 records each. They have 85 records in common and I want to get the values which are in list1 but not in list2 in a seperate list say list_out1 and the values which are in list2 but not in list1 in another list say list_out2. I finally want to print list_out1 and list_out2. I tried to do as given in this answer but it is giving me all the values in list1 while trying to print list_out1. Also, I have tried using foreach loop and if condition as below and it is also giving me all the values in list1 to print list_out1.

foreach ($i in $list1)
{
   if($list2 -notcontains $i) {
      $i
    }
}

I don't know where am I doing wrong. The logic seems to be alright for me. Correct me if I am wrong.

Upvotes: 5

Views: 17183

Answers (3)

Drew
Drew

Reputation: 4020

Using the Compare-Object is what you are after. Assuming you do $List1.Item or something similar.

$MissingGroups = Compare-Object -ReferenceObject $List1 -DifferenceObject $List2 -Property $Item | Where-Object{$_.sideIndicator -eq "<="}

Upvotes: 8

user6811411
user6811411

Reputation:

I don't see the proplem you have with the Q&A you linked yourself.

Using the example lists from postanote's good answer and Compare-Object

## Q:\Test\2018\11\15\SO_53313785.ps1
$List1 = 'Hello','World','Today','FromList1'
$List2 = 'Hello','World','Today','FromList2'
compare $List1 $list2

This returns (using alias compare for Copare-Object and relying on positional parameter 1 for -ReferenceObject and 2 for -DifferenceObject)

InputObject SideIndicator
----------- -------------
FromList2   =>
FromList1   <=

You can use the SideIndicator to determine to which file the output should be appended.

Compare-Object -ReferenceObject $List1 -DifferenceObject $List2 |
    ForEach-Object -Begin {
        Remove-item '.\UniqueToList*.txt'
    } -Process {
       if ($_.SideIndicator -eq '<='){
           Add-Content -Path '.\UniqueToList1.txt' -Value $_.InputObject
       } else {
           Add-Content -Path '.\UniqueToList2.txt' -Value $_.InputObject
       }
    }

In case of more complex list objects you might use Export-Csv with the -Append parameter instead.

Upvotes: 1

postanote
postanote

Reputation: 16086

Do you mean this? If you just want to the screen, just remove the Out-File stuff.

get the values which are in list1 but not in list2 in a seperate list say list_out1

$List1 = 'Hello','World','Today','FromList1'
$List2 = 'Hello','World','Today','FromList2'

# get the values which are in list1
ForEach($Item in $List1)
{
    If($List2 -notcontains $Item)
    {$Item | Out-File -FilePath D:\Temp\ListOne.txt -Append} 
} 

# Results in the file

FromList1

and the values which are in list2 but not in list1 in another list say list_out2.

ForEach($Item in $List2)
{
    If($List1 -notcontains $Item)
    {$Item | Out-File -FilePath D:\Temp\ListTwo.txt -Append} 
} 

# Results in the file

FromList2

Upvotes: 2

Related Questions