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