Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Add column to CSV object

Posted on 2013-01-15
3
466 Views
Last Modified: 2013-01-31
I am using the Import-CSV cmdlet to import a CSV file, what I am looking for is a way I can add columns to the spreadsheet without exporting/importing. Say I have imported a spreadsheet with columns A,B,C I want to be able to add D,E,F and be able to use these as normal in powershell.

Ideas?
0
Comment
Question by:Blowfelt82
  • 2
3 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 38778208
empty columns?

assume this csv file:

id,age
1,11
2,22
3,33
4,44

now, you want to use import-csv and add empty column "name" after age:

$csvFile = gc c:\temp\test.csv
$csvFile[0] += ",name"
$csv = $csvFile| convertfrom-csv
$csv 

Open in new window


result:

screenshot
0
 

Author Comment

by:Blowfelt82
ID: 38778526
I can see what its trying to do but I think it cannot be applied to the object returned by the cmdlet 'Import-CSV'. When I run the code above I get a 'Method invocation failed because [System.Management.Automation.PSObject] doesnt contain a methid named 'op_Addition'.

Looks like '+=' is a no go with the Import-CSV object.
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 38778603
i know, that's why i use gc.
there's no 'fency' way doing it without export and import.
this is the only way which u avoid export-import the entire csv object.
the twick here is reading it as a text file (via gc) and convert it to csv object.
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Set OWA language and time zone in Exchange for individuals, all users or per database.
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question