$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?
Technology Tips and News
$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?
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 }
http://blog.pointbeyond.com/2011/07/29/powershell-csv-and-array-handling/
Should be the array value
$profile[0].id
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?
foreach ($id in $profile)
{
$id.id
}
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.
How about $profile.ORG? Also no values, or does it perhaps return the ID values?
wizend