Solved

working with csv objects in powershell

Posted on 2014-01-31
10
872 Views
Last Modified: 2014-01-31
I've been creating a psobject containing two properties and exporting to a csv.
I want to be able to import that data back in, add another record, sort it and then recreate the csv.

How can I do this, preferably in both ver 2 & 3?

Customer Age
-------       -------
John          25


$obj = new-object psobject
$obj | add-member noteproperty CustName "John"
$obj | add-member noteproperty CustAge "25"
$obj | export-csv agedata.csv


$CustInfo = import-csv agedata.csv

$newcust's name and age will be function [string]parameters

$CustInfo += $newCust does not work for me (have seen several examples showing this)
0
Comment
Question by:sirbounty
  • 7
  • 3
10 Comments
 
LVL 39

Expert Comment

by:footech
ID: 39824820
As long as $CustInfo is an array $CustInfo += $newCust will work.
If you only exported a single object, of course when read back it will be a single object.  You can convert it with
$CustInfo = @($CustInfo)
and then the following will work.
$CustInfo += $newCust
0
 
LVL 67

Author Comment

by:sirbounty
ID: 39824894
I've tried several ways and I end up with:

Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition'.
0
 
LVL 67

Author Comment

by:sirbounty
ID: 39824904
Here's a rough example of what I'm trying to pull off...

function newData {
    [Cmdletbinding()]
    param(
    [parameter(Mandatory=$true)]
    [string]$CustName,

    [parameter(mandatory=$true)]
    [string]$CustAge
    )

    process {
        $obj = New-Object psobject
        $obj | add-member noteproperty CustName $CustName
        $obj | add-member noteproperty CustAge $CustAge
        Write-Output $obj
    }
    
}


$obj = new-object psobject
$obj | add-member noteproperty CustName "John"
$obj | add-member noteproperty CustAge "25"
$obj | export-csv agedata.csv

$CustInfo = Import-Csv agedata.csv
$newCust = newData "Ted" "33"

$combineData = $CustInfo + $newCust
$combineData

Open in new window

0
 
LVL 39

Accepted Solution

by:
footech earned 250 total points
ID: 39824916
I got that too until I ran
$CustInfo = @($CustInfo)
So the order would be
$CustInfo = import-csv agedata.csv
$CustInfo = @($CustInfo)
$newcust = new-object psobject -Property @{ CustName="hank"; CustAge="23"}
$CustInfo += $newcust

Open in new window

0
 
LVL 67

Author Comment

by:sirbounty
ID: 39824917
and of course, these end up being simply pscustomobjects...
I'm open to outputting them a different way, but I need to be able to
 -write to a csv formatted file
 -later read it in
 -add a new entry to it
 -sort it based on the first column
 -re-save/overwrite original csv
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 67

Author Closing Comment

by:sirbounty
ID: 39824921
Oh, I thought I'd tried that and it didn't work...
It seems to have this time, however.  Thanks
0
 
LVL 67

Author Comment

by:sirbounty
ID: 39824980
Well, it sort of works...

Any idea why it's doing this?
$CustInfo = @(Import-Csv agedata.csv)
$newCust = newData "Ted" "33"

$combineData = $CustInfo + $newCust
$combineData | sort custname | export-csv agedata.csv -notype

Open in new window


then the file contains:
"custname","custage"
"",
"John","23"
"Tom","41"


I don't know where that extra blank line is coming from...
0
 
LVL 67

Author Comment

by:sirbounty
ID: 39824994
What's weirder still is it doesn't seem to do it every time... :(
0
 
LVL 39

Expert Comment

by:footech
ID: 39825209
I can't replicate what you're seeing.  Sounds like an empty line/blank record is getting in there, but I don't see where.
0
 
LVL 67

Author Comment

by:sirbounty
ID: 39825308
Fiddled with it and finally got it working...no idea on the problem though.  thanks again.
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

Suggested Solutions

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 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

19 Experts available now in Live!

Get 1:1 Help Now