Home > Software engineering >  PowerShell Invoke-Sqlcmd results to csv using CovertTo-Csv
PowerShell Invoke-Sqlcmd results to csv using CovertTo-Csv

Time:04-28

Given:

  • PowerShell 5.1
  • PowerShell SqlServer module - Invoke-Sqlcmd

How do I get the actual values of the column into csv?

Write-Output $sqlResult.Quantity | ConvertTo-Csv

Gives me:

#TYPE System.Int32

When it should be:

100,200

CodePudding user response:

The problem, which I honestly don't know why it is a problem, is that ConvertTo-CSV does not convert arrays to comma delimited strings.

So you have to use a different tool such as join, which converts each item in the array to a string and joins those strings with commas:

$sqlResult.Quantity -join ','
  • Related