Welcome Forums General PowerShell Q&A Powershell

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

3 years, 6 months ago.

  • Author
  • #27707

    Points: 0
    Rank: Member

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

  • #27713

    Points: 428
    Helping Hand
    Rank: Contributor

    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

    Points: 0
    Rank: Member

    Thank you Rob. It worked.

The topic ‘Powershell’ is closed to new replies.