Export data from MSSQL to csv with Powershell

  • Useful when you need to export data from MSSQL into csv with a script.
  • At first you need to add sql server snapin.
add-pssnapin sqlserverprovidersnapin100
add-pssnapin sqlservercmdletsnapin100
  • Now you can export your data.
$Server = "serverName"
$DBName = "dataBase"
$QueryFmt= "SELECT * FROM Mytable"
$OutputFile = "output.csv"
Invoke-Sqlcmd -ServerInstance $Server -Database $DBName -Query $QueryFmt | Export-CSV $OutputFile
programming/powershell/mssqlexport2csv.txt · Last modified: 2018-06-21 19:48 (external edit)
CC Attribution-Noncommercial-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0