# Unable to -append to .csv with new-object

**URL:** <https://forums.powershell.org/t/unable-to-append-to-csv-with-new-object/2160>\
**Category:** PowerShell Help\
**Created:** [December 22, 2013, 3:41pm UTC](https://forums.powershell.org/t/unable-to-append-to-csv-with-new-object/2160 "2013-12-22T15:41:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthew-hellen](https://avatars.discourse-cdn.com/v4/letter/m/eb8c5e/32.png) [@matthew-hellen](https://forums.powershell.org/u/matthew-hellen)\
**Post date:** [December 22, 2013, 3:41pm UTC](https://forums.powershell.org/t/unable-to-append-to-csv-with-new-object/2160/1 "2013-12-22T15:41:54Z")

</div>

All,  
I have a .csv with three columns A B and C  
Column A is named “Serial”  
Colum B is named “Customer”  
Column C is named “Seat”

The csv will have existing content and I only need to append data which will be added via my powershell script.  
within the script I am creating new objects the following way

```
$newData = @{'Serial' = 10; 'Customer' = 'NewCustomerName'; 'Seat' = 100}
$a = new-object -TypeName PsObject -Property $newData
$a |export-data "c:\location\thecsv.csv" -append
```

I get an error:  
Export-Csv : Cannot append CSV content to the following file: C:\test\PSTesting.csv. The  
appended object does not have a property that corresponds to the following column:  
Serial. To continue with mismatched properties, add the -Force parameter, and then retry  
the command.  
At line:1 char:5

- $a |Export-Csv C:\test\PSTesting.csv -Append
- 

```
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

```

  - CategoryInfo : InvalidData: (Serial:String) [Export-Csv], InvalidOperatio  
nException
  - FullyQualifiedErrorId : CannotAppendCsvWithMismatchedPropertyNames,Microsoft.Power  
Shell.Commands.ExportCsvCommand

I am sure that those properties exist AND that there is no typo.  
What the problem seems to be is when I do a get-member against the $a its not showing any of those three properties.  
It will display them when I just type $a but it will ‘Serial’, ‘Customer’, or ‘Seat’ as a property in GM.  
Any thoughts on how to create the object so that the properties are part of it?

This me prototyping to see if I can use PS at scale with a living csv file (I know pretty bad). And not being able to figure out why new objects aren’t listing properties in GM is driving me crazy.

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [December 22, 2013, 3:59pm UTC](https://forums.powershell.org/t/unable-to-append-to-csv-with-new-object/2160/2 "2013-12-22T15:59:58Z")

</div>

Well, your code snippet says Export-Data, but I’ll assume you’re using Export-CSV?

And the problem is indeed that the CSV contains a “Serial” column but, at the time you’re using it, the object(s) in $a don’t contain a Serial property. Your use of Get-Member proves it. So let me run through this on my end.

If I just take your three lines of code and run them, with no pre-existing CSV file, I get a CSV file with one data row. If I run the code again, it runs without error, and the CSV contains both data lines. So your approach here is fine. You’ll notice, if you do that, that the CSV file contains a #comment row listing the data type that you produced - a PSCustomObject. Or that’s what happens to me - can you confirm that’s what happens to you?

Now, if I use Notepad to make this CSV file manually:

```
"Serial","Customer","Seat"
"10","NewCustomerName","100"
"10","NewCustomerName","100"
```

And then I run your three lines of code again, I still get no error. The CSV lacks the comment row still, but I successfully appended a third line of data to it.

So in short, this works for me in v3. What am I doing differently from you?

And yeah, a “living” CSV file is just awful. Too bad you can’t use something sensible like an actual database, or even XML, which can at least be modified programmatically. All this mucking about with text. Yuck. Ah well.

---

<div class="post-metadata">

**Author:** ![matthew-hellen](https://avatars.discourse-cdn.com/v4/letter/m/eb8c5e/32.png) [@matthew-hellen](https://forums.powershell.org/u/matthew-hellen)\
**Post date:** [December 23, 2013, 3:58am UTC](https://forums.powershell.org/t/unable-to-append-to-csv-with-new-object/2160/3 "2013-12-23T03:58:36Z")

</div>

wow  
this is just so confusing. I’m was doing all my protyping on a windows 8.1 Enterprise edition and the appending nor creating csv was working. However, once ran on a win 7 v3 machine the steps mentioned did work…  
Is the powershell on enterprise 8.1 different?

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [December 23, 2013, 4:27am UTC](https://forums.powershell.org/t/unable-to-append-to-csv-with-new-object/2160/4 "2013-12-23T04:27:22Z")

</div>

PowerShell is no different across editions, but 8.1 comes with v4, not v3. So there are obviously version differences. Although I ran this in v3 myself on Win8.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:52pm UTC](https://forums.powershell.org/t/unable-to-append-to-csv-with-new-object/2160/5 "2024-05-16T20:52:32Z")

</div>


