Invoke-sqlcmd output without "quotes" and headers

Moek picture Moek · Mar 27, 2017 · Viewed 10.1k times · Source

Output example:

#TYPE System.Data.DataRow <---
"PersonalNr" <---
"00001005"
"00001008"
"00001009"
"00001013"
"00001019"
"00001024"

Requirements:

I want a output without the 2 first lines and without the quote symbols, how can i do that?

For the headers I tried the options -h -1 but the output is still the same. Is it even possible with Sqlcmd?

Current Code:

Invoke-Sqlcmd -ServerInstance SERVER -h -1 -Query $QueryFmt | Export-CSV $AttachmentPath

Answer

dbso picture dbso · Mar 27, 2017

This code should work:

Invoke-Sqlcmd -ServerInstance "" -Database "" -Query "" | ConvertTo-Csv -NoTypeInformation -Delimiter "," | Select-Object -Skip 1 | % {$_ -replace '"', ""} | Out-File ("C:\test.csv") -Force -Encoding ascii

Invoke-Sqlcmd produces:

PersonalNr       
---    
00001005  
00001001  
00001006  
00001007  
00001008 

With Export-Csv, the file looks like:

#TYPE System.Data.DataRow
"PersonalNr"
"00001005"
"00001001"
"00001006"
"00001007"
"00001008"

Using my above mentioned code, I get the file looking like this:

00001005
00001001
00001006
00001007
00001008