Compare two lists in Powershell










0















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.










share|improve this question
























  • If the logic is right, then your types are wrong. What kind of records are they? Whatever it is, two things which appear the same are not comparing as the same thing - the typical PowerShell beginner mistake is to want strings or ints, but get PSCustomObjects by doing something like get-childitem | select name or get-childitem | ft name instead of the more correct get-childitem | select -expandproperty name..

    – TessellatingHeckler
    Nov 15 '18 at 7:15












  • What types are your lists / records? Mind that two object instances are treated as different, even if they might have the same property values.

    – marsze
    Nov 15 '18 at 8:39











  • @TessellatingHeckler, How should we deal if there are a slight variations in each record in those lists i mean, one list has value 'ABC' and the other list has 'aB.C'. But I want my function to treat both of same. How do we do data cleaning and perform the required comparision?

    – Sree
    Nov 15 '18 at 19:57











  • or data something like this $List1 = 'Hello','World2','Today','FromList1' $List2 = 'Hello1','World','Todayi','FromList2' compare $List1 $list2

    – Sree
    Nov 15 '18 at 20:46












  • @Sree you'll need to be very clear about what "data cleaning" means. Removing numbers and punctuation would be easy enough ($text -replace 'W') will do it. and PowerShell ignores uppercase/lowercase when doing comparisons, so that's OK. But what is the rule where Today matches Todayi? That's going past "data cleaning" and into "spell checker" or "dictionary lookup with probabilities" or "natural language processing magic"...

    – TessellatingHeckler
    Nov 16 '18 at 10:23
















0















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.










share|improve this question
























  • If the logic is right, then your types are wrong. What kind of records are they? Whatever it is, two things which appear the same are not comparing as the same thing - the typical PowerShell beginner mistake is to want strings or ints, but get PSCustomObjects by doing something like get-childitem | select name or get-childitem | ft name instead of the more correct get-childitem | select -expandproperty name..

    – TessellatingHeckler
    Nov 15 '18 at 7:15












  • What types are your lists / records? Mind that two object instances are treated as different, even if they might have the same property values.

    – marsze
    Nov 15 '18 at 8:39











  • @TessellatingHeckler, How should we deal if there are a slight variations in each record in those lists i mean, one list has value 'ABC' and the other list has 'aB.C'. But I want my function to treat both of same. How do we do data cleaning and perform the required comparision?

    – Sree
    Nov 15 '18 at 19:57











  • or data something like this $List1 = 'Hello','World2','Today','FromList1' $List2 = 'Hello1','World','Todayi','FromList2' compare $List1 $list2

    – Sree
    Nov 15 '18 at 20:46












  • @Sree you'll need to be very clear about what "data cleaning" means. Removing numbers and punctuation would be easy enough ($text -replace 'W') will do it. and PowerShell ignores uppercase/lowercase when doing comparisons, so that's OK. But what is the rule where Today matches Todayi? That's going past "data cleaning" and into "spell checker" or "dictionary lookup with probabilities" or "natural language processing magic"...

    – TessellatingHeckler
    Nov 16 '18 at 10:23














0












0








0








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.










share|improve this question
















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.







powershell scripting powershell-v3.0






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 8:37









marsze

5,53232041




5,53232041










asked Nov 15 '18 at 6:41









SreeSree

1077




1077












  • If the logic is right, then your types are wrong. What kind of records are they? Whatever it is, two things which appear the same are not comparing as the same thing - the typical PowerShell beginner mistake is to want strings or ints, but get PSCustomObjects by doing something like get-childitem | select name or get-childitem | ft name instead of the more correct get-childitem | select -expandproperty name..

    – TessellatingHeckler
    Nov 15 '18 at 7:15












  • What types are your lists / records? Mind that two object instances are treated as different, even if they might have the same property values.

    – marsze
    Nov 15 '18 at 8:39











  • @TessellatingHeckler, How should we deal if there are a slight variations in each record in those lists i mean, one list has value 'ABC' and the other list has 'aB.C'. But I want my function to treat both of same. How do we do data cleaning and perform the required comparision?

    – Sree
    Nov 15 '18 at 19:57











  • or data something like this $List1 = 'Hello','World2','Today','FromList1' $List2 = 'Hello1','World','Todayi','FromList2' compare $List1 $list2

    – Sree
    Nov 15 '18 at 20:46












  • @Sree you'll need to be very clear about what "data cleaning" means. Removing numbers and punctuation would be easy enough ($text -replace 'W') will do it. and PowerShell ignores uppercase/lowercase when doing comparisons, so that's OK. But what is the rule where Today matches Todayi? That's going past "data cleaning" and into "spell checker" or "dictionary lookup with probabilities" or "natural language processing magic"...

    – TessellatingHeckler
    Nov 16 '18 at 10:23


















  • If the logic is right, then your types are wrong. What kind of records are they? Whatever it is, two things which appear the same are not comparing as the same thing - the typical PowerShell beginner mistake is to want strings or ints, but get PSCustomObjects by doing something like get-childitem | select name or get-childitem | ft name instead of the more correct get-childitem | select -expandproperty name..

    – TessellatingHeckler
    Nov 15 '18 at 7:15












  • What types are your lists / records? Mind that two object instances are treated as different, even if they might have the same property values.

    – marsze
    Nov 15 '18 at 8:39











  • @TessellatingHeckler, How should we deal if there are a slight variations in each record in those lists i mean, one list has value 'ABC' and the other list has 'aB.C'. But I want my function to treat both of same. How do we do data cleaning and perform the required comparision?

    – Sree
    Nov 15 '18 at 19:57











  • or data something like this $List1 = 'Hello','World2','Today','FromList1' $List2 = 'Hello1','World','Todayi','FromList2' compare $List1 $list2

    – Sree
    Nov 15 '18 at 20:46












  • @Sree you'll need to be very clear about what "data cleaning" means. Removing numbers and punctuation would be easy enough ($text -replace 'W') will do it. and PowerShell ignores uppercase/lowercase when doing comparisons, so that's OK. But what is the rule where Today matches Todayi? That's going past "data cleaning" and into "spell checker" or "dictionary lookup with probabilities" or "natural language processing magic"...

    – TessellatingHeckler
    Nov 16 '18 at 10:23

















If the logic is right, then your types are wrong. What kind of records are they? Whatever it is, two things which appear the same are not comparing as the same thing - the typical PowerShell beginner mistake is to want strings or ints, but get PSCustomObjects by doing something like get-childitem | select name or get-childitem | ft name instead of the more correct get-childitem | select -expandproperty name..

– TessellatingHeckler
Nov 15 '18 at 7:15






If the logic is right, then your types are wrong. What kind of records are they? Whatever it is, two things which appear the same are not comparing as the same thing - the typical PowerShell beginner mistake is to want strings or ints, but get PSCustomObjects by doing something like get-childitem | select name or get-childitem | ft name instead of the more correct get-childitem | select -expandproperty name..

– TessellatingHeckler
Nov 15 '18 at 7:15














What types are your lists / records? Mind that two object instances are treated as different, even if they might have the same property values.

– marsze
Nov 15 '18 at 8:39





What types are your lists / records? Mind that two object instances are treated as different, even if they might have the same property values.

– marsze
Nov 15 '18 at 8:39













@TessellatingHeckler, How should we deal if there are a slight variations in each record in those lists i mean, one list has value 'ABC' and the other list has 'aB.C'. But I want my function to treat both of same. How do we do data cleaning and perform the required comparision?

– Sree
Nov 15 '18 at 19:57





@TessellatingHeckler, How should we deal if there are a slight variations in each record in those lists i mean, one list has value 'ABC' and the other list has 'aB.C'. But I want my function to treat both of same. How do we do data cleaning and perform the required comparision?

– Sree
Nov 15 '18 at 19:57













or data something like this $List1 = 'Hello','World2','Today','FromList1' $List2 = 'Hello1','World','Todayi','FromList2' compare $List1 $list2

– Sree
Nov 15 '18 at 20:46






or data something like this $List1 = 'Hello','World2','Today','FromList1' $List2 = 'Hello1','World','Todayi','FromList2' compare $List1 $list2

– Sree
Nov 15 '18 at 20:46














@Sree you'll need to be very clear about what "data cleaning" means. Removing numbers and punctuation would be easy enough ($text -replace 'W') will do it. and PowerShell ignores uppercase/lowercase when doing comparisons, so that's OK. But what is the rule where Today matches Todayi? That's going past "data cleaning" and into "spell checker" or "dictionary lookup with probabilities" or "natural language processing magic"...

– TessellatingHeckler
Nov 16 '18 at 10:23






@Sree you'll need to be very clear about what "data cleaning" means. Removing numbers and punctuation would be easy enough ($text -replace 'W') will do it. and PowerShell ignores uppercase/lowercase when doing comparisons, so that's OK. But what is the rule where Today matches Todayi? That's going past "data cleaning" and into "spell checker" or "dictionary lookup with probabilities" or "natural language processing magic"...

– TessellatingHeckler
Nov 16 '18 at 10:23













3 Answers
3






active

oldest

votes


















2














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 "<="





share|improve this answer






























    1














    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


    # 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)
    Out-File -FilePath D:TempListTwo.txt -Append


    # Results in the file

    FromList2





    share|improve this answer























    • Why don't you just use Compare-Object?

      – bluuf
      Nov 15 '18 at 8:51











    • No disagreement, but the OP wanted to pump to two different files, with individual results, not just a compare. So, just taking the OP at their explicit request.

      – postanote
      Nov 15 '18 at 20:33


















    0














    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:Test20181115SO_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.






    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313785%2fcompare-two-lists-in-powershell%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      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 "<="





      share|improve this answer



























        2














        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 "<="





        share|improve this answer

























          2












          2








          2







          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 "<="





          share|improve this answer













          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 "<="






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 7:01









          DrewDrew

          1,417418




          1,417418























              1














              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


              # 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)
              Out-File -FilePath D:TempListTwo.txt -Append


              # Results in the file

              FromList2





              share|improve this answer























              • Why don't you just use Compare-Object?

                – bluuf
                Nov 15 '18 at 8:51











              • No disagreement, but the OP wanted to pump to two different files, with individual results, not just a compare. So, just taking the OP at their explicit request.

                – postanote
                Nov 15 '18 at 20:33















              1














              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


              # 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)
              Out-File -FilePath D:TempListTwo.txt -Append


              # Results in the file

              FromList2





              share|improve this answer























              • Why don't you just use Compare-Object?

                – bluuf
                Nov 15 '18 at 8:51











              • No disagreement, but the OP wanted to pump to two different files, with individual results, not just a compare. So, just taking the OP at their explicit request.

                – postanote
                Nov 15 '18 at 20:33













              1












              1








              1







              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


              # 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)
              Out-File -FilePath D:TempListTwo.txt -Append


              # Results in the file

              FromList2





              share|improve this answer













              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


              # 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)
              Out-File -FilePath D:TempListTwo.txt -Append


              # Results in the file

              FromList2






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 15 '18 at 8:14









              postanotepostanote

              3,7862410




              3,7862410












              • Why don't you just use Compare-Object?

                – bluuf
                Nov 15 '18 at 8:51











              • No disagreement, but the OP wanted to pump to two different files, with individual results, not just a compare. So, just taking the OP at their explicit request.

                – postanote
                Nov 15 '18 at 20:33

















              • Why don't you just use Compare-Object?

                – bluuf
                Nov 15 '18 at 8:51











              • No disagreement, but the OP wanted to pump to two different files, with individual results, not just a compare. So, just taking the OP at their explicit request.

                – postanote
                Nov 15 '18 at 20:33
















              Why don't you just use Compare-Object?

              – bluuf
              Nov 15 '18 at 8:51





              Why don't you just use Compare-Object?

              – bluuf
              Nov 15 '18 at 8:51













              No disagreement, but the OP wanted to pump to two different files, with individual results, not just a compare. So, just taking the OP at their explicit request.

              – postanote
              Nov 15 '18 at 20:33





              No disagreement, but the OP wanted to pump to two different files, with individual results, not just a compare. So, just taking the OP at their explicit request.

              – postanote
              Nov 15 '18 at 20:33











              0














              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:Test20181115SO_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.






              share|improve this answer





























                0














                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:Test20181115SO_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.






                share|improve this answer



























                  0












                  0








                  0







                  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:Test20181115SO_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.






                  share|improve this answer















                  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:Test20181115SO_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.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 15 '18 at 15:19

























                  answered Nov 15 '18 at 12:52









                  LotPingsLotPings

                  19.5k61533




                  19.5k61533



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313785%2fcompare-two-lists-in-powershell%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      政党

                      天津地下鉄3号線