title: How to import data from .csv in SQL Server using PowerShell?
body: <p>I'm using PowerShell and have to import data from a .csv file into a already created table on a SQL Server Database. So I don't need the header line from the csv, just write the data.</p>
  <p>Here is what I have done so far:</p>
  <pre class="lang-ps prettyprint-override"><code>#Setup for SQL Server connection
  #SQL Server Name
  $SQLServer = &quot;APPLIK02\SQLEXPRESS&quot;
  #Database Name
  $SQLDBName = &quot;code-test&quot;
  
  #Create the SQL Connection Object
  $SQLConn = New-Object System.Data.SQLClient.SQLConnection
  #Create the SQL Command Object, to work with the Database
  $SQLCmd = New-Object System.Data.SQLClient.SQLCommand
  
  #Set the connection string one the SQL Connection Object
  $SQLConn.ConnectionString = &quot;Server=$SQLServer;Database=$SQLDBName; Integrated Security=SSPI&quot;
  #Open the connection
  $SQLConn.Open()
  
  #Handle the query with SQLCommand Object
  $SQLCmd.CommandText = $query
  #Provide the open connection to the Command Object as a property
  $SQLCmd.Connection = $SQLConn
  
  #Execute 
  $SQLReturn=$SQLCmd.ExecuteReader()
  
  Import-module sqlps
  $tablename = &quot;dbo.&quot;+$name
  
  Import-CSV .\$csvFile | ForEach-Object Invoke-Sqlcmd 
    -Database $SQLDBName -ServerInstance $SQLServer 
    #-Query &quot;insert into $tablename VALUES ('$_.Column1','$_.Column2')&quot;
  #Close
  $SQLReturn.Close()  
  $SQLConn.Close()
  </code></pre>