Adding columns to an existing CSV

Welcome Forums General PowerShell Q&A Adding columns to an existing CSV

This topic contains 5 replies, has 5 voices, and was last updated by

2 years, 7 months ago.

  • Author
  • #12541

    Points: 0
    Rank: Member

    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.


  • #12542

    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero
    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.

  • #12543

    Points: 0
    Rank: Member

    This worked perfectly. Thank you!

  • #32820

    Points: 0
    Rank: Member

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

  • #45955

    Points: 0
    Rank: Member

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

  • #45957

    Points: 21
    Rank: Member

    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'}}

The topic ‘Adding columns to an existing CSV’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort