title: Compare 2 csv to create list of unique items in Powershell
body: <p>I have 2 csv files and need to create a 3rd.</p>
  
  <p><strong>csv_1</strong> (only 1 column)</p>
  
  <pre><code>USERNAME
  guy1
  guy2
  guy3
  </code></pre>
  
  <p><strong>csv_2</strong></p>
  
  <pre><code>"ID","USERNAME","GROUPU"
  "1","guyA","Default"
  "2","guy1","Default"
  "3","guyB","Default"
  "4","guy3","Default"
  "5","guyC","Default"
  </code></pre>
  
  <p>for an output from comparing these two tables I'd like to see:</p>
  
  <pre><code>USERNAME,ID
  guy1,2
  guy3,4
  </code></pre>
  
  <p>I tried this and it dumped far too much data (listing all entries multiple times)</p>
  
  <pre><code>$file1 = Import-Csv csv_1.csv
  $file2 = Import-Csv csv_2.csv
  
  foreach ($line in $file1){
      $U1 = $line.User
      foreach ($line in $file2)
          $U2 = $line.USERNAME
          $I2 = $line.ID
          If ($U1 = $U2){
              $x = $U2+","+$I2
              ac temp.csv$x
              }
          }
      }
  </code></pre>
  
  <p>I added a break:outside line after the ac temp.csv $x line but then it only listed 1 entry multiple times.</p>
  
  <p>I also tries this (based on <a href="https://stackoverflow.com/questions/21433993/compare-two-column-or-more-column-in-csv-and-merge-match-data-in-new-csv">this</a> entry):</p>
  
  <pre><code>$file1 = Import-Csv csv_1.csv
  $file2 = Import-Csv csv_2.csv
  
  ForEach($Record in $file1){
      $MatchedValue = (Compare-Object $file2 $Record -Property "USERNAME" -IncludeEqual -ExcludeDifferent -PassThru).value
      $Record = Add-Member -InputObject $Record -Type NoteProperty -Name "SQLID" -Value $MatchedValue
      }
  $file1 #|Export-Csv ".\Combined.csv" -NoTypeInformation
  </code></pre>
  
  <p>but didn't get anything in the SQLID column and it included some values in $file1 that are not in $file2.  I figure the lack of SQLID value is because I need to add a variable there (the -value just being $matchedValue which is just the line from $file1), but I'm not sure how to get the variable from the correct record in $file2.</p>
  
  <p>Suggestions?</p>