# Adding columns to an existing CSV

**URL:** <https://forums.powershell.org/t/adding-columns-to-an-existing-csv/2196>\
**Category:** PowerShell Help\
**Created:** [January 14, 2014, 5:09am UTC](https://forums.powershell.org/t/adding-columns-to-an-existing-csv/2196 "2014-01-14T05:09:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![john-gatto](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@john-gatto](https://forums.powershell.org/u/john-gatto)\
**Post date:** [January 14, 2014, 5:09am UTC](https://forums.powershell.org/t/adding-columns-to-an-existing-csv/2196/1 "2014-01-14T05:09:04Z")

</div>

Hello all -

We’re in the process of identifying all of our users who are still on Windows XP. I have been given a csv file of about 958 users. It contains their SAMAccountName and workstation names. To make our desktop support team’s job easier, I’m trying to output a CSV that retains the workstation names as well as their full name (get-aduser -properties Name) and their location, which is stored in an extended attribute in AD (ExtensionAttribute1). The end result would need to be a CSV that contains the workstation and SAMAccount Names which we already have, and the additional FullName and Location (EA1) properties.

I’m kind of at a loss on where to start. Would I need to create a custom array and then a custom object? These are two areas that I’m a little weak in, so any ideas would be greatly appreciated.

Thanks!

---

<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:** [January 14, 2014, 5:37am UTC](https://forums.powershell.org/t/adding-columns-to-an-existing-csv/2196/2 "2014-01-14T05:37:34Z")

</div>

```
Import-CSV original.csv |
ForEach-Object {

  $data1 = 'whatever'
  $data2 = 'hello'

  $_ | 
  Add-Member -MemberType NoteProperty -Name ColumnA -Value $data1 -PassThru |
  Add-Member -MemberType NoteProperty -Name ColumnB -Value $data2 -PassThru

} |
Export-CSV revised.csv
```

Something vaguely like that. You can access the current columns via $_, e.g., "$user = Get-ADUser -Identity $_.samaccountname" if the current CSV has a samaccountname column. Import-CSV brings in the CSV as objects, Add-Member adds properties to those objects, and Export-CSV writes the revised objects back to a CSV.

---

<div class="post-metadata">

**Author:** ![john-gatto](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@john-gatto](https://forums.powershell.org/u/john-gatto)\
**Post date:** [January 14, 2014, 7:35am UTC](https://forums.powershell.org/t/adding-columns-to-an-existing-csv/2196/3 "2014-01-14T07:35:07Z")

</div>

This worked perfectly. Thank you!

---

<div class="post-metadata">

**Author:** ![bob-hodges](https://avatars.discourse-cdn.com/v4/letter/b/e19adc/32.png) [@bob-hodges](https://forums.powershell.org/u/bob-hodges)\
**Post date:** [December 10, 2015, 12:23am UTC](https://forums.powershell.org/t/adding-columns-to-an-existing-csv/2196/4 "2015-12-10T00:23:49Z")

</div>

Thanks for the reply Don, this post helped me a lot!

---

<div class="post-metadata">

**Author:** ![bojan-zivkovic](https://avatars.discourse-cdn.com/v4/letter/b/7feea3/32.png) [@bojan-zivkovic](https://forums.powershell.org/u/bojan-zivkovic)\
**Post date:** [July 6, 2016, 6:31am UTC](https://forums.powershell.org/t/adding-columns-to-an-existing-csv/2196/5 "2016-07-06T06:31:52Z")

</div>

Is it possible to save changes to an existing .csv file? I tried but then .csv became empty.

---

<div class="post-metadata">

**Author:** ![dan-potter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-potter/32/138_2.png) [@dan-potter](https://forums.powershell.org/u/dan-potter)\
**Post date:** [July 6, 2016, 7:18am UTC](https://forums.powershell.org/t/adding-columns-to-an-existing-csv/2196/6 "2016-07-06T07:18:52Z")

</div>

You can simplify this adding an expression to the data you retrieve from the csv. I’m a scratch scripting golfer 🙂

[pscustomobject]@{one=‘firstcol’;two=‘secondcol’} | select one,two,@{n=‘more’;e={‘moredata’}}

---

<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:40pm UTC](https://forums.powershell.org/t/adding-columns-to-an-existing-csv/2196/7 "2024-05-16T20:40:54Z")

</div>

Closed on May 16, 2024, 8:40pm UTC


