Export data from MSSQL to csv with Powershell

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