Solved

Add column to CSV object

Posted on 2013-01-15
3
463 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now