title: PowerShell - For each - duplicate
body: <p>I have a PowerShell script with a <code>foreach</code> loop. The Write-Host within this always writes duplicate on the screen. I added a send-mail and it sends duplicate email per record. What am I doing wrong here?</p>
  
  <pre><code>Import-module ActiveDirectory
  
  $web = get-SPWeb "http://inside.nov.com/sales"
  $list = $web.Lists["Shared Documents"]
  $now = get-date;
  $SearchBase = "OU=Sales,DC=NOV,DC=com"
  $LdapServer = "AD748.NOV.COM"
  $From = "intranet@NOV.com"
  $To = "Shawna_Richardson@NOV.com"
  $Cc = "TBD"
  $Subject = "Testing Powershell email"
  $Body = "Active Directory updates"
  $SMTPServer = "smtp74.NOV.com"
  $CAML = '
     &lt;Where&gt;
       &lt;Eq&gt;
          &lt;FieldRef Name="Region" /&gt;
          &lt;Value Type="Choice"&gt;Northwest&lt;/Value&gt;
       &lt;/Eq&gt;
     &lt;/Where&gt;
  '
  $query = new-object Microsoft.SharePoint.SPQuery
  $query.RowLimit = 10
  $query.Query = $CAML
  $listitems = $list.GetItems($query)
  $count = $listitems.count
  Write-Host $count
  
  function Get-Data()
  {
      if ($count -ge 1)
      {
          foreach($item in $listitems)
          {
              $NOVEmpID = $item["NOVEmpID"]
  
              if($item["NOVEmpID"] -ne $null)
              {
                  $LDAPResult = Get-ADUser -SearchBase $searchbase -SearchScope 'subtree' -Server $ldapserver -filter "NOVEmpID -like '*$NOVEmpID*'" -Properties * | Select -Property AccountExpirationDate, userAccountControl
                  $pair1, $pair2 = $LDAPResult -replace '^@{|}$' -split '; '
                  $null, $ADExpDate = $pair1 -split '=', 2
                  $null, $userAccountControl = $pair2 -split '=', 2
                  if($ADExpDate){
                      $ADExpDate = [DateTime]::Parse($ADExpDate).ToShortDateString()
                  }
                  $SPEndDate = [DateTime]::Parse($item["SPExpDate"]).ToShortDateString()
  
                  $data = @{
                              "Emp ID" = $item["NOVEmpID"]
                              "Sales Name" = $item["EmployeeName"]
                              "SP End Date" = $item["SPExpDate"]
                              "AD End Date" = $ADExpDate
                              "AD Account Status" = $userAccountControl
                           }
                  New-Object PSObject -Property $data
                  if($ADExpDate)
                  {
                      if($ADExpDate -gt $SPEndDate)
                      {
                          write-host $item["NOVEmpID"] $item["EmployeeName"]    $item["SPExpDate"] $ADExpDate
                          $item["NewDate"] = $ADExpDate
                          $item.SystemUpdate()
                          $list.update()
                      }
                      if($ADExpDate -lt $SPEndDate)
                      {
                          $subject = $item["EmployeeName"] + " - " + $NOVEmpID + " date is greater than AD Date"
                          $Body = $item["EmployeeName"] + " - " + $NOVEmpID + " - "  + $item["SPExpDate"] + " - " + $ADExpDate
                          Send-MailMessage -From $From -to $To -Subject $subject -Body $Body -SmtpServer $SMTPServer
                      }
                  }
                  else
                  {
                      $subject = $item["EmployeeName"] + " - " + $NOVEmpID + " AD Date is empty"
                      $Body = $item["EmployeeName"] + " - " + $NOVEmpID + " - "  + $item["SPExpDate"] + " - " + $ADExpDate
                      Send-MailMessage -From $From -to $To -Subject $subject -Body $Body -SmtpServer $SMTPServer
                  }
              }
          }
      }
  }
  $csvFileForFacilities = "$((Get-Date).ToString('MM-dd-yyyy')).csv"
  Write-Host $csvFileForFacilities
  Get-Data | Export-Csv -NoTypeInformation -Path "G:\\Result\\export.csv"
  Get-Data | Out-File $csvFileForFacilities
  $web.dispose()
  </code></pre>