Solved

Add column to CSV object

Posted on 2013-01-15
3
471 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

623 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