title: Duplicate SQL results from Powershell CSV export
body: <p>I am trying to output SQL results to a .csv file using Powershell separated by its respective column.</p>
  <p>The script I wrote works, but it will duplicate the same result three times in the csv. Even if I have only 1 result from the <code>Select</code> statement from the table, it will output it three times in the .csv file.</p>
  <p>I tried using <code>pscustomobject</code> as well. But it throws me an error and does not output anything.</p>
  <pre><code>Clear-Variable Results
  Clear-Variable Report
  [string] $query = &quot;Select Name, Value From options with(nolock) where Name IN('ExportFolder','ImportFolder','GlobalExportFolder'); Select @@ROWCOUNT AS AffectedRows&quot;
  
  [string[]] $servers = @('sqlinstance=mytestdb') 
   
  foreach($server in $servers)
  {
      $instance = ($server -split '=')[0]
      $db = ($server -split '=')[1]
  
          Try{
                  $Results = Invoke-Sqlcmd -ServerInstance $instance -Database $db -Query $query                 
                 
                  $ExportFolder = ($Results.ItemArray[1])
                  $GlobalExportFolder = ($Results.ItemArray[3])
                  $ImportFolder = ($Results.ItemArray[5]) 
  
                  $Array = '$ExportFolder','$GlobalExportFolder','ImportFolder'               
  
                  $mail = $Array | Select-Object @{n=&quot;SQLServer&quot;;e={$instance}},@{n=&quot;DBName&quot;;e={$db}}, @{n=&quot;ExportFolder&quot;;e={$ExportFolder}}, @{n=&quot;GlobalExportFolder&quot;;e={$GlobalExportFolder}}, @{n=&quot;ImportFolder&quot;;e={$ImportFolder}} 
  
                  $mail | Export-Csv -Path &quot;C:\Users\localadmin\Documents\Logs\HostNameCheck.csv&quot; -NoTypeInformation -Append -Verbose    
  
                  $Report += $Results  
                  $Report | Select Name, Value | Export-Csv -Path &quot;C:\Users\localadmin\Documents\Logs\SFTPHostnameModificationCheck.csv&quot; -NoTypeInformation -Append -Verbose                
                  
                  Clear-Variable Results
                  Clear-Variable Report           
             } 
             Catch {                     
                  Write-Host (&quot;Error: Data retrieval failed against instance $instance for $db&quot; + &quot; - &quot; + (Get-Date)) -ForegroundColor Red 
                  Write-Output (&quot;Error: Data retrieval failed against $instance on $db&quot; + &quot; - &quot; + (Get-Date)) | Out-File -FilePath $PathFailedLogs -Append                      
             }
  } 
  
  $attachment = Get-ChildItem -Path &quot;C:\Users\localadmin\Documents\Logs&quot; -Include *.csv -Recurse -Force
  
  Send-MailMessage -From &quot;test@test.com&quot; -To &quot;localadmin@nonprod.com&quot; -Subject &quot;SFTPHostnameCheck&quot; -SmtpServer &quot;localrelay@local.com&quot; -Attachments $attachment
  </code></pre>
  <p>Using PSCustomObject</p>
  <pre><code>$obj = New-Object [PSCustomObject] -Property @{
        'SQLServer' = $instance
        'DBName' = $db
        'ExportFolder' = &quot;$ExportFolder
        'GlobalExportFolder' = $GlobalExportFolder
        'ImportFolder' = $ImportFolder&quot;
  }
  
  $list += $obj 
  $list | Export-Csv -Path &quot;C:\Users\localadmin\Documents\Logs\HostNamecheck.csv&quot; -NoTypeInformation -Append -Verbose
  </code></pre>