Solved

Add column to CSV object

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
A procedure for exporting installed hotfix details of remote computers using powershell
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

786 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