title: How to format output when exporting SQL query to CSV
body: <p>I have a task to save the results of a SQL Server query into a <code>.csv</code> file. After some googling I decided to use PowerShell. I found a script, modified it a bit, it works and almost all is ok.</p>
  
  <pre><code>$server = "server"
  $database = "database"
  $query = "SELECT * from et_thanks"
  
  $tod = Get-Date;
  $file = "{0:yyyyMMdd}_go.csv" -f $tod;
  $extractFile = @"
  \\info\export_files\$file
  "@
  
  $connectionTemplate = "Data Source={0};Integrated Security=SSPI;Initial Catalog={1};"
  $connectionString = [string]::Format($connectionTemplate, $server, $database)
  $connection = New-Object System.Data.SqlClient.SqlConnection
  $connection.ConnectionString = $connectionString
  
  $command = New-Object System.Data.SqlClient.SqlCommand
  $command.CommandText = $query
  $command.Connection = $connection
  
  $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
  $SqlAdapter.SelectCommand = $command
  $DataSet = New-Object System.Data.DataSet
  $SqlAdapter.Fill($DataSet)
  $connection.Close()
  
  $DataSet.Tables[0] | Export-Csv -Force -Delimiter ";" $extractFile
  </code></pre>
  
  <p>But I have 2 problems which I can't solve:</p>
  
  <ol>
  <li><p>When I open the <code>.csv</code> file I see columns headers and commented string on first line:</p>
  
  <pre><code>#TYPE System.Data.DataRow
  "ob_no","c_name","c_visible","c_fp","e_from","e_to"
  "436439","09.09.2013 11:29:08","0","","10937","260153"
  </code></pre>
  
  <p>How can I get rid of it?</p></li>
  <li><p>All values are surrounded with quotes. Is it possible to modify script not to use it while exporting? Autoreplace isn't good idea, cause there is a possibility that quote symbol can be found in sql data. </p></li>
  </ol>
  
  <p>I tried to find answers in documentation (<a href="http://ss64.com/ps/export-csv.html" rel="nofollow">http://ss64.com/ps/export-csv.html</a>) but with no success.</p>