26 September,2019 by Rambler
Question: I'm trying to use powershell cmdlet export-csv to output a csv file - but without the header information from a sql server recordset i.e first row
I'm trying to implement this powershell code snippet.
$outputfile="\\myoutputfile.csv"
$dt = new-object "System.Data.DataTable"
--add some data into the data table dt | select * -ExcludeProperty RowError, RowState, HasErrors, Name, Table, ItemArray | export-csv -path $outputfile -NoTypeInformation
Answer: It is possible to get the no header on an export-csv - but it does require a few workarounds - which seem excessive for what you're attempting to achieve
I prefer to use the powershell cmdlet ConvertTo-Csv. The ConvertTo-Csv cmdlet is piped and then the -Skip parameter is used . Easy !
You may be asking what is the difference between ConvertTo-Csv and export-csv. Typically Export refers to directly exporting out to that file , whereas ConvertTo refers converting to that format - e.g ConvertTo-csv
Taking your example , here is a reworking .
$dt | select * -ExcludeProperty RowError, RowState, HasErrors, Name, Table, ItemArray |ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1 | Set-Content -path $outputfile
So really this post should be titled - How to use Powershell export-csv without a header by using ConvertTo-Csv
Read more on powershell and csv
Export-CSV Powershell (SQL Server DBA)
Use Powershell import-csv to enumerate list items from a CSV array ...
This is only a preview. Your comment has not yet been posted.
As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.
Having trouble reading this image? View an alternate.
Posted by: |