13

Trying to figure out how to add a row to a csv file with titles. I get the content using:

$fileContent = Import-csv $file -header "Date", "Description"

$File returns

Date,Description
Text1,text2 
text3,text4

How do I append a row with a new date and description. Sorry I am relatively new to powershell. thanks to anyone that can help

Ken White
  • 123,280
  • 14
  • 225
  • 444
Ken
  • 131
  • 1
  • 1
  • 3
  • 1
    Welcome to StackOverflow. Please be more careful when choosing the tags for your questions. I edited yours to be more appropriate to your question. (Tags are used for grouping questions into categories by topic or subject, and your original tag of `stackoverflow.com` was not only inappropriate, but had absolutely nothing to do with your question.) You might also want to visit the [FAQ](http://stackoverflow.com/faq) to familiarize yourself with the site in general. :) – Ken White Mar 21 '12 at 21:38

5 Answers5

20

To simply append to a file in powershell,you can use add-content.

So, to only add a new line to the file, try the following, where $YourNewDate and $YourDescription contain the desired values.

$NewLine = "{0},{1}" -f $YourNewDate,$YourDescription
$NewLine | add-content -path $file

Or,

"{0},{1}" -f $YourNewDate,$YourDescription | add-content -path $file

This will just tag the new line to the end of the .csv, and will not work for creating new .csv files where you will need to add the header.

marceljg
  • 997
  • 4
  • 14
  • Thank you marceljg. I was exactly looking for the same solution and your one liner works perfect for me. – 300 Nov 19 '18 at 21:54
  • just wondering is there a simple way to prepend the added rows rather then appending? – Georodin Dec 13 '20 at 13:59
15

Create a new custom object and add it to the object array that Import-Csv creates.

$fileContent = Import-csv $file -header "Date", "Description"
$newRow = New-Object PsObject -Property @{ Date = 'Text4' ; Description = 'Text5' }
$fileContent += $newRow
Andy Arismendi
  • 50,577
  • 16
  • 107
  • 124
  • This worked great for me except that it changed the ordering of my columns. After I appended the line using the above answer I had to use `$Import = $Import | Select-Object...` to re-order my columns for export. – mack Mar 21 '14 at 18:46
9

I know this is an old thread but it was the first I found when searching. The += solution did not work for me. The code that I did get to work is as below.

#this bit creates the CSV if it does not already exist
$headers = "Name", "Primary Type"
$psObject = New-Object psobject
foreach($header in $headers)
{
 Add-Member -InputObject $psobject -MemberType noteproperty -Name $header -Value ""
}
$psObject | Export-Csv $csvfile -NoTypeInformation

#this bit appends a new row to the CSV file
$bName = "My Name"
$bPrimaryType = "My Primary Type"
    $hash = @{
             "Name" =  $bName
             "Primary Type" = $bPrimaryType
              }

$newRow = New-Object PsObject -Property $hash
Export-Csv $csvfile -inputobject $newrow -append -Force

I was able to use this as a function to loop through a series of arrays and enter the contents into the CSV file.

It works in powershell 3 and above.

corky
  • 185
  • 2
  • 8
0

Simple to me is like this:

$Time = Get-Date -Format "yyyy-MM-dd HH:mm K"
$Description = "Done on time"

"$Time,$Description"|Add-Content -Path $File # Keep no space between content variables

If you have a lot of columns, then create a variable like $NewRow like:

$Time = Get-Date -Format "yyyy-MM-dd HH:mm K"
$Description = "Done on time"
$NewRow = "$Time,$Description" # No space between variables, just use comma(,).

$NewRow | Add-Content -Path $File # Keep no space between content variables

Please note the difference between Set-Content (overwrites the existing contents) and Add-Content (appends to the existing contents) of the file.

Rafiq
  • 1,380
  • 4
  • 16
  • 31
0

I find using $newRowToAdd | Export-Csv -Append the easiest way. Not sure which version that was introduced though

Sebastian
  • 6,293
  • 6
  • 34
  • 47