I have some large files in CSV format that I would like to turn into objects using the Import-Csv
command in Powershell. The files are missing the column headings however and these need to be added before I use Import-Csv
. What is the fastest and most efficient way of adding these to the file?
Prepending to a large file is not an easy or quick operation.
However, import-csv does have a "header" argument that you can use to specify the column headers, i.e.:
$header = 'Foo', 'Bar', 'Baz'
import-csv .\myfile.csv -header $header
This one liner works nicely.
@("Header Text") + (get-content myFile.txt) | set-content myFile.txt
Explanation
@("Header Text")
creates an array with one item. + (get-content myFile.txt)
creates a new array with the contents of both. | set-content myFile.txt
pipes the result to set-content and saves to file.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With