Solved

Add column to CSV object

Posted on 2013-01-15
3
467 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
[X]
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
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Synchronize a new Active Directory domain with an existing Office 365 tenant
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

735 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