add-pssnapin sqlserverprovidersnapin100 add-pssnapin sqlservercmdletsnapin100
$Server = "serverName" $DBName = "dataBase" $QueryFmt= "SELECT * FROM Mytable" $OutputFile = "output.csv" Invoke-Sqlcmd -ServerInstance $Server -Database $DBName -Query $QueryFmt | Export-CSV $OutputFile