Skip to content
Advertisement

Powershell, invoke-sqlcmd, export-csv fails to show data if there is more than one result

I have the below code to get data from a SQL DB and export it into a CSV file:

This works perfectly if there is one result. But if there is more than one result, it will show the below in the csv file

enter image description here

I am at my wits end as to why it is doing this. It’s obviously the DB parameter data or something to that effect. Been googling for a few days with no luck. Anyone smarter than I able to assist please?

Advertisement

Answer

Instead of using Select-Object to rename your columns, which is quite inefficient, you could give the alias to your columns on the query itself:

Also, as in my comment, the code you have on your question is fine and should work, the only problem was using -InputObject instead of piping the results to Export-Csv:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement