This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Akshay Salunke Akshay Salunke 1 year, 3 months ago.

  • Author
  • #27707
    Profile photo of Akshay Salunke
    Akshay Salunke

    How can we add columns to csv and update only first row of the same csv file?

  • #27713
    Profile photo of Rob Simmers
    Rob Simmers

    You would actually Import the CSV, update the object, and then export the csv. If we had a csv with the following data:

    Age Name
    --- ----
     22 John
     21 Sue 

    We want to get the CSV and select only the first line, add a property for Computername and then export the csv:

    $myCsv = Import-CSV C:\Temp\mycsv.csv | Select -First 1
    $myCsv | Add-Member -MemberType NoteProperty -Name ComputerName -Value $env:COMPUTERNAME
    $myCsv | Export-CSV -Path C:\temp\mycsv.csv -NoTypeInformation

    Now your CSV would look like this:

    Age Name ComputerName 
    --- ---- ------------ 
    22  John LENOVOY50-ROB

    Keep in mind that there are many ways to do things, so with the limited information you are providing, this would work.

  • #27718
    Profile photo of Akshay Salunke
    Akshay Salunke

    Thank you Rob. It worked.

You must be logged in to reply to this topic.