Powershell - Export data to a CSV file

Опубликовано: 18 Июль 2026
на канале: SQLDataEngineer
14
0

A short instruction how to export data from a table in SQL server to a CSV file with Powershell module DBATool and the command Invoke-DbaQuery.

A few examples with a query or procedure:

##Basic query example##
$query = "select top 100 * from Customer"
Invoke-DbaQuery -SqlInstance "localhost" -Database TestDB -CommandType Text -Query $query | Export-Csv -Path "C:\Temp\ExportData\Export_SqlServer.csv" `
-Delimiter ";" -NoTypeInformation


##Procedure exempel##
Invoke-DbaQuery -SqlInstance "localhost" -Database TestDB -CommandType StoredProcedure -Query 'dbo.usp_getCustomer' `
-SqlParameter @{ Name = "Roy" } | Export-Csv -Path "C:\Temp\ExportData\Export_SqlServerProcedure.csv" `
-Delimiter ";" -NoTypeInformation