title: Use Powershell to save a datatable as a csv
body: <p>There has to be an easy way to do this. I have a Powershell script which connects to a database and I want to save the resulting datatable as a CSV. Here is the code so far:</p>
  
  <pre><code>$connString = "Provider=msdaora;Data Source=MyDatabase;User Id=test;Password=test    
  $qry = "select * from employees"
  $OLEDBConn = New-Object System.Data.OleDb.OleDbConnection($connString)            
  $OLEDBConn.open()            
  $readcmd = New-Object system.Data.OleDb.OleDbCommand
  $readcmd.Connection = $OLEDBConn
  $readcmd.CommandTimeout = '300'
  $readcmd.CommandText = $qry
  $da = New-Object system.Data.OleDb.OleDbDataAdapter($readcmd)            
  $dt = New-Object system.Data.datatable            
  [void]$da.fill($dt)            
  $OLEDBConn.close()
  </code></pre>