# Export Object to CSV

**URL:** <https://forums.powershell.org/t/export-object-to-csv/9040>\
**Category:** PowerShell Help\
**Created:** [July 19, 2017, 2:01pm UTC](https://forums.powershell.org/t/export-object-to-csv/9040 "2017-07-19T14:01:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mitzoid1](https://avatars.discourse-cdn.com/v4/letter/m/0ea827/32.png) [@mitzoid1](https://forums.powershell.org/u/mitzoid1)\
**Post date:** [July 19, 2017, 2:01pm UTC](https://forums.powershell.org/t/export-object-to-csv/9040/1 "2017-07-19T14:01:54Z")

</div>

When I run the following code with write-output $obj at the end It prints all of the computers on the screen but when I try to export to csv I only get (one computer) the last computer information in the csv file.

```
$Computers = get-content C:\POSH\test.txt
foreach ($comp in $Computers) {
  $CS = Get-WmiObject -Class Win32_computersystem -ComputerName $comp
  $Encl = Get-WmiObject -Class Win32_Systemenclosure -ComputerName $comp

  $props = @{CompName=$cs.name
             Manufacturer=$cs.manufacturer
             Model=$cs.model
             Serial=$encl.serialnumber}

    $obj = New-Object -TypeName PSObject -property $props
    Write-Output $obj
 # $obj | export-csv c:\posh\test.csv -Notype (I only get one computer when I replace write-output with this)
}
```

---

<div class="post-metadata">

**Author:** ![johnnyfive](https://avatars.discourse-cdn.com/v4/letter/j/ea666f/32.png) [@johnnyfive](https://forums.powershell.org/u/johnnyfive)\
**Post date:** [July 19, 2017, 2:32pm UTC](https://forums.powershell.org/t/export-object-to-csv/9040/2 "2017-07-19T14:32:14Z")

</div>

You need to use the -append switch. That will add the output to the end of the file.

---

<div class="post-metadata">

**Author:** ![mitzoid1](https://avatars.discourse-cdn.com/v4/letter/m/0ea827/32.png) [@mitzoid1](https://forums.powershell.org/u/mitzoid1)\
**Post date:** [July 19, 2017, 2:47pm UTC](https://forums.powershell.org/t/export-object-to-csv/9040/3 "2017-07-19T14:47:30Z")

</div>

thanks! 🙂 that worked great. so simple

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [July 21, 2017, 1:31pm UTC](https://forums.powershell.org/t/export-object-to-csv/9040/4 "2017-07-21T13:31:38Z")

</div>

Or put the export last outside the loop:

```
} | export-csv c:\posh\test.csv -Notype
```

---

<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:37pm UTC](https://forums.powershell.org/t/export-object-to-csv/9040/5 "2024-05-16T20:37:51Z")

</div>


