I'm a beginner with Powershell trying to query a SQL Server database and output the csv in a file organised rows and columns. I can partially achieve this with the following command :
sqlps -Command Invoke-Sqlcmd -ServerInstance "xxx.xx.xxx.xxx"
-Database "xxxx" -InputFile "xxx" | out-file -filepath "xxxx"
My problem is that the output from the query doesn't come in rows and columns, instead it groups each row together as a list of strings, one string per cell. For example:
column a: value 1
column b: value 1
column c: value 1
column a: value 2
column b: value 2
column c: value 2
I have also tried using export-csv, however this just returns the length of each string returned by the command above, exactly like this issue: Export csv spits out length only
I have tried a group-object, but I'm struggling to make it work. If somebody could explain conceptually what I'm trying to do with some clear guidelines it would be very much appreciated.