title: Using PowerShell DataTable to import data into SQL Server table
body: <p>I need to import a <code>.csv</code> file with 350 columns into a SQL Server 2008 R2 database.</p>
  
  <p>Surfing the internet I came up with a function called <code>Out-DataTable</code> from Chad Miller: <a href="https://gallery.technet.microsoft.com/scriptcenter/4208a159-a52e-4b99-83d4-8048468d29dd" rel="nofollow">link to script on Technet</a></p>
  
  <p>I have used this to complete the PowerShell script as follows:</p>
  
  <pre><code>#----------------------------------------------------------------
  $logNaam=MylogFileName
  TRY
  {
      $ConnectionString = "Data Source=mySQLServer; Initial Catalog=MyDB; Trusted_Connection=True;";
      . ".\Out-DataTable.ps1"
  
      $csvDataTable = Import-CSV -Path "MyCSV_FileAndPath" | Out-DataTable
      $bulkCopy = new-object ("Data.SqlClient.SqlBulkCopy") $ConnectionString
      $bulkCopy.DestinationTableName = "to_MyTableName"
      $bulkCopy.WriteToServer($csvDataTable)
  }
    Catch
  {
      $ErrorMessage = $_.Exception.Message
      $FailedItem = $_.Exception.ItemName
        "Error: Job starten lukt niet : ($ErrorMessage) " | out-file $logNaam -append
          "Error: Job starten lukt niet : ($FailedItem) " | out-file $logNaam -append
  }
  Finally
  {}
  
  #----------------------------------------------------------------
  </code></pre>
  
  <p>Running this code in the PowerShell command prompt, I get this error:</p>
  
  <blockquote>
    <p>Unexpected token 'data' in expression or statement.<br>
    At .\MyScript.ps1:20 char:27<br>
    +     $ConnectionString = "Data &lt;&lt;&lt;&lt;  Source=MySQLServer; Initial Catalog=MyDB
        ; Trusted_Connection=True;";<br>
    + CategoryInfo          : ParserError: (data:String) [], ParseException<br>
    + FullyQualifiedErrorId : UnexpectedToken  </p>
  </blockquote>
  
  <p>I'm stuck with this and have no clue where to start. Changing <code>Data Source=MySQLServer</code> to <code>Server=MySQLServer</code> does not solve it. The message only changed pointing to the position following MySQLServer&lt;&lt;&lt;. </p>
  
  <p>Also adding the Windows server-name to it (<code>MyWinServer\MySQLServer</code>) did not help, either.</p>
  
  <p>Any advise, directions, explanation or help to code this script would by much appreciated. </p>