CSV return value

$file1="D:\file\input.csv"  
$profile = Import-Csv -Path $file1 -Header ID,ORG,CUSTOMER,Name

The value of ID,ORG,CUSTOMER,Name are in the csv file.

$profile.ID doesnt return any value. Any idea why?

 

August 26th, 2013 9:56pm

What version of PowerShell are you running, and how many records are in the CSV file?  If you're using v2 and the CSV contains multiple records, then $profile is an array, and you can't access the "ID" property of the objects in the array that way.  How you access the ID values depends on what you intend to do with them, but here are some options:

$file1="D:\file\input.csv"  
$profile = Import-Csv -Path $file1 -Header ID,ORG,CUSTOMER,Name 

$profile | Select-Object -ExpandProperty ID
$profile |
ForEach-Object {
    $_.ID
}

foreach ($object in $profile)
{
    $object.ID
}


Free Windows Admin Tool Kit Click here and download it now
August 26th, 2013 10:06pm

http://blog.pointbeyond.com/2011/07/29/powershell-csv-and-array-handling/

Should be the array value

$profile[0].id

August 26th, 2013 10:25pm

Ok, thanks.

Instead of....

$ORG=$profile | Select-Object -ExpandProperty ORG

$ID=$profile | Select-Object -ExpandProperty ID

$CUSTOMER=$profile | Select-Object -ExpandProperty CUSTOMER

Is there an easier way of doing this?

Free Windows Admin Tool Kit Click here and download it now
August 26th, 2013 10:42pm

foreach ($id in $profile)
{
  $id.id
}
August 27th, 2013 12:24am

It just depends on what you plan to do with the information.  When you're using the Select-Object -ExpandProperty command, you'll be getting back an array of values in $ORG, $ID, and $CUSTOMER, which may not be what you intended.  If you're planning to do something with each record in the CSV (accessing those properties of each individual record), go with the "foreach" loop that has been suggested.
Free Windows Admin Tool Kit Click here and download it now
August 27th, 2013 12:59am

Be aware that $profile is a PowerShell automatic variable and using it as you show will make its original value unavailable to the scope in which you change it.

August 27th, 2013 11:12am

How about $profile.ORG? Also no values, or does it perhaps return the ID values?

wizend

Free Windows Admin Tool Kit Click here and download it now
August 27th, 2013 11:41am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics