13

I'm doing a script to export data from a SQL Server database. I want to output a .csv file with as delimiter ";".

Here is my script:

#Variable to hold variable  
$SQLServer = "SERVEUR"  
$SQLDBName = "TOTO"  
$uid ="PS"  
$pwd = "password123"   
$delimiter = ";"

#SQL Query  
$SqlQuery = "SELECT * from $SQLDBName.dbo.SAGE_TO_PRESTASHOP;"  
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection  
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True;"  
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand  
$SqlCmd.CommandText = $SqlQuery  
$SqlCmd.Connection = $SqlConnection  
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter  
$SqlAdapter.SelectCommand = $SqlCmd   
#Creating Dataset  
$DataSet = New-Object System.Data.DataSet  
$SqlAdapter.Fill($DataSet)  
$DataSet.Tables[0] | Out-File "E:\EXPORTS\export.csv"  
3
  • Out-File -> Export-Csv -Delimiter ';' Commented Dec 12, 2018 at 11:21
  • Export-Csv documentation. Note the -Delimiter parameter. Commented Dec 12, 2018 at 11:21
  • Export-Csv has a delimiter parameter. Commented Dec 12, 2018 at 11:21

1 Answer 1

18

Replace your bottom line with this:

$DataSet.Tables[0] | export-csv -Delimiter $delimiter -Path "E:\EXPORTS\export.csv" -NoTypeInformation 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.