Powershell write into a specific column csv


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 ?



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.

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.



OK, but you are not using them!! :wink: 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!.

Ty for answering
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

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

Hmmm … I’m pretty unsure if I got what you want to achieve. :wink: 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? :wink:
$Data | Export-Csv -Path D:\sample\sample.csv -NoTypeInformation