Reputation: 89
I have a Powershell script that produces a .csv file with names and email addresses. I would like to be able to add a row with a title to the top of the file.
I can do this with a text file but for some reason I am not successful with a .csv file. Suggestions ?
[string]$FileDS = Get-Date -Format "yyyyMMdd"
[string]$outFile = "${outpath}\${members}_${FileDS}.csv"
New-Item -ItemType file $outfile -Force
$object | export-csv -path $outfile
Upvotes: 0
Views: 8979
Reputation: 3814
There is an IETF standard RFC 4180 that specifies how to generate a CSV file and what it can and cannot contain. To answer your question directly, there is no way to add a "title" in a CSV file. To summarize the specification, your file needs to have the following
As you can see there is no standard way to set a "title". However if you are referring to a "header" as a "title" you can add it by using the following
$file = import-csv $outfile -Header a , b , c | export-csv $file
Where a, b, c are your headers.
Your data should end up looking like the following
Header1, Header2, Header3 CRLF
value11, value12, value13 CRLF
value21, value22, value23 CRLF
value31, value32, value32
The header row is indicated as something separate from data rows by including the following mimetype parameter
text/csv; header=present
As opposed to
text/csv; header=absent
As you can tell the only restrictions are the mimetype parameter, the delimiter, and the carriage return. Field data is surrounded with double quotes if the data in that field contains a line break, comma, and or double quote. To break down the Mimetype a bit more the media type is text, the subtype is csv, and two optional parameters: header and charset.
Since the specification explicitly outlines the special / reserved characters as well as the optional parameters I would feel safe saying there is no way to add a title row.
Since your initial post was referring to a "title" which is undefine in the specification I did my best to answer your question to the best of my ability. CSV Format is a specific instantiation of Tabluar Data standard. There are several different tabular data formats, and organizations such as W3C and OKI have created standardizations that include more functionality than the RFC 4180 standard.
You might want to look into CSV Schema which I believe is part of the OKI standard. Again this is all based on standards so it really depends on what is consuming and parsing this CSV file.
If you have any additional questions feel free to add a comment and I will do my best to respond.
The following links will bring you to the official specification for three tabular data formats. When in doubt I would definitely use the IETF standard since it is by far the most widely adopted, however I have included the W3C and the OKI specification just in case your consuming application supports it
Upvotes: 2
Reputation: 1283
If you would like to add to the CSV top row:
[array]$newRow = "Everything on the new row"
$csv = Get-Content $outFile
$newCSV = $newRow + $csv | Out-File $outFile
To add to a CSV without removing the old data:
$newRow | Out-File $outFile -Append
I hope this helps you.
Upvotes: 1