Powershell write into a specific column csv

Welcome Forums General PowerShell Q&A Powershell write into a specific column csv

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

 
Participant
1 month ago.

  • Author
    Posts
  • #171091

    Participant
    Topics: 1
    Replies: 2
    Points: 17
    Rank: Member

    Hi,

    Im trying to do a script that write into a specific column in a csv but i didnt find how to do this.

    Actually i have :

    $arrayDates = @("2010", "2011", "2019")

    Add-Content -path file.csv -value ($arrayDates[0] + ", " + $arrayDates[1] + ", " + $arrayDates[2]) #here i create my columns

    And then i want to write some datas in "2010" column for example. How can i do this ?

    Thx

     

  • #171160

    Participant
    Topics: 1
    Replies: 1528
    Points: 2,581
    Helping Hand
    Rank: Community Hero

    If you want to work with structured data (tables, CSV files) you should use the proper cmdlets like Import-CSV, Export-CSV, ConvertFrom-CSV, ConvertTo-CSV ...
    You should read the complete help including the examples to learn how to use it.

  • #171280

    Participant
    Topics: 1
    Replies: 2
    Points: 17
    Rank: Member

    Yes i know these commands-lets, but in the documentation i dont found what i want to do. I just want to write into a specific columns of a csv file. Im new in powershell and i've not knowledges to make this.

     

    Ty,

  • #171307

    Participant
    Topics: 1
    Replies: 1528
    Points: 2,581
    Helping Hand
    Rank: Community Hero

    Yes i know these commands-lets, b ....

    OK, but you are not using them!! 😉 Show the code where you use them and what you tried o accomplish your task and we will try to help you.

    When you post code you should use the code tag button to format your code as code. Read the very first post on top of the list of this forrum to learn how to do : Read Me Before Posting! You'll be Glad You Did!.

  • #171340

    Participant
    Topics: 1
    Replies: 2
    Points: 17
    Rank: Member
    New-Item file.csv #i create my csv file because i work on a empty csv file in my case
    $arrayDates = @("2010", "2011", "2019") #here i create my columns
    Add-Content -path file.csv -value ($arrayDates[0] + ", " + $arrayDates[1] + ", " + $arrayDates[2]) #adding my columns to csv file
    #and if i want to see the results i do
    $csv = import-csv
    $csv
    
    # output is :
    2010 2011    2019
    ---- ----    ----
    
    
    The problem is i want to add some data in 2010 for exemple
    I only use import-csv because I do not need others or I do not know how to use them in my case
  • #171358

    Participant
    Topics: 1
    Replies: 1528
    Points: 2,581
    Helping Hand
    Rank: Community Hero

    Hmmm ... I'm pretty unsure if I got what you want to achieve. 😉 Where do your data come from? It does not make that much sense to create an empty csv file with no data. But anyway you could even that achieve easier ...

    $Data = [PSCustomObject]@{
        2010 = ''
        2011 = ''
        2019 = ''
    } 
    $Data # this line is only to show the data on the console ... and it looks the same like yours, right?  ;-)
    $Data | Export-Csv -Path D:\sample\sample.csv -NoTypeInformation

You must be logged in to reply to this topic.