Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two list and find names that are in list one and not list two using powershell

Just wondering if you can help me out.. I am trying to compare two list(txt file) and find strings that are in list A and not in List B and output it to another txt file.. anybody know how to do it using powershell ?

Here is what I have so far:

Compare-Object -ReferenceObject $FolderLists -DifferenceObject $AdUserName -passThru

I would like to find all strings that are in $FolderLists and not $AdUserName and possibly output it to another variable. The issue I am having is that it outputs strings that are not in both lists.

like image 557
tyson619 Avatar asked Sep 25 '13 18:09

tyson619


1 Answers

I assume $FolderList and $AdUserName are arrays of strings? You don't really need Compare-Object to compare arrays. It's as simple as this:

$FolderList | ?{$AdUserName -notcontains $_}

 

Compare-Object is for comparing the specified properties of collections of objects with common properties. You could do this with Compare-Object if you really want, like this:

Compare-Object $FolderList $AdUserName | ?{$_.SideIndicator -eq '<='} | Select-Object -ExpandProperty InputObject

But as you can see, it's overkill for this task.

To output the result to another variable, simply assign it:

$AnotherVariable = $FolderList | ?{$AdUserName -notcontains $_}
like image 113
Adi Inbar Avatar answered Oct 04 '22 01:10

Adi Inbar