Is there an easy way to run a MySQL query from the PowerShell command line and output the results into a csv formatted file?

This question is the same as How to output MySQL query results in CSV format? except in Windows. I had to figure it out in PowerShell but my answer didn't belong on the linux question. So here's the Windows+PowerShell sibling.

up vote 0 down vote accepted

Stan's answer from How to output MySQL query results in CSV format?, adapted for Windows PowerShell

mysql my_database_name -u root | Out-File .\my_output_file.csv

This gives me a mysql prompt, but without the usual mysql > at the start. I type:

source C:\Aboslute\Path\With Spac es\Without\Quotes\To\my_select_statement.sql

It gives an error message and exits if there is a problem with the command, or gives me the empty prompt if the command executed successfully. I type exit to finish up.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.