title: PowerShell: Remove duplicated items from array
body: <p>I am trying to locate discrepancies in BIND DNS records.  I would like to output a CSV file that only has those discrepancies.  I have a CSV file that has all records from all locations in BIND (ns.prvt, ns.pub, common, includes).  What I'm trying to figure out is how to output a CSV that only shows the discrepancies.  For 2 records to be considered a discrepancy, they must meet the following criteria:</p>
  
  <ol>
  <li>Both records have the same RecordName and RecordType.</li>
  <li>Both records have different Data or TTL.</li>
  <li>Both records come from different locations.</li>
  </ol>
  
  <p>I am almost there with the following script but it keeps showing me a couple of rows that don't necessarily meet the above criteria.  </p>
  
  <pre><code>$Records = Import-Csv C:\Temp\Domain_ALL.csv | Select * | Sort Data,Location
  $RecordsRev = @()
  $Records | % {
      $Record = $_
      $Records | % {
          $DataFE = $_
          If (
          ([string]($Record | ? {($_.RecordName -eq $DataFE.RecordName)}).RecordName -eq $DataFE.RecordName) -and 
          ([string]($Record | ? {($_.RecordName -eq $DataFE.RecordName)}).RecordType -eq $DataFE.RecordType) -and 
          ([string]($Record | ? {($_.RecordName -eq $DataFE.RecordName)}).Location -ne $DataFE.Location) -and 
          (([string]($Record | ? {($_.RecordName -eq $DataFE.RecordName)}).Data -ne $DataFE.Data) -or 
          ([string]($Record | ? {($_.RecordName -eq $DataFE.RecordName)}).TTL -ne $DataFE.TTL))
          ) {
              $RecordsRev += $_
          }
      }
  }
  $RecordsRev | Export-Csv C:\Temp\Domain_Discrepancies.csv -NoType
  </code></pre>
  
  <p>The results that I get are:</p>
  
  <pre><code>RecordName RecordType Data                           TTL Location
  ---------- ---------- ----                           --- --------
  domain.com TXT        "MS=abc1234566"                600 Includes
  domain.com TXT        "MS=abc1234566"                600 Common  
  domain.com TXT        "site-verification=abcd1234"   600 Includes
  domain.com TXT        "site-verification=abcd1234"   600 Common  
  www        CNAME      somedomain.com.test.           600 Includes
  www        CNAME      somedomain.com.                600 Common
  </code></pre>
  
  <p>The results that I expect are:</p>
  
  <pre><code>RecordName RecordType Data                           TTL Location
  ---------- ---------- ----                           --- -------- 
  www        CNAME      somedomain.com.test.           600 Includes
  www        CNAME      somedomain.com.                600 Common
  </code></pre>
  
  <p>How do I delete all duplicated rows in the array?  This is different from "Select * -unique" as I don't want to keep any row that contains the duplicated information. </p>
  
  <p>EDIT:  I think the main problem is that, since the script checks each record against every record in the CSV, it technically is a discrepancy.  For example, in the below table, record 1 meets the criteria to be a discrepancy because it differs from record 4.  However, since record 1 is the same as record 2, it should actually be omitted from the results. </p>
  
  <pre><code>RecordNumber RecordName RecordType Data                           TTL Location
  ------------ ---------- ---------- ----                           --- --------
  1            domain.com TXT        "MS=abc1234566"                600 Includes
  2            domain.com TXT        "MS=abc1234566"                600 Common  
  3            domain.com TXT        "site-verification=abcd1234"   600 Includes
  4            domain.com TXT        "site-verification=abcd1234"   600 Common  
  5            www        CNAME      somedomain.com.test.           600 Includes
  6            www        CNAME      somedomain.com.                600 Common
  </code></pre>
  
  <p>Any help would be greatly appreciated.  </p>
  
  <p>Kyle</p>