Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to edit NoteProperty names containing space created by IMPORT-

611 views
Skip to first unread message

Zardos42

unread,
Mar 8, 2010, 7:51:01 PM3/8/10
to
I use Powershell a lot to process bulk data exported from SAP and other
systems. The export interfaces create CSVs with headings which contain
spaces. This works fine but it limits the ability to reference the objects.

A exported field might be called "Employee Number".
IMPORT-CSV creates an object with a NoteProperty called "Employee Name"

Now

$obj."Employee Name" is not valid

$obj.EmployeeName would work nicely

Is there a way to modify NoteProperty -name after IMPORT-CSV
or
Is there a way to make IMPORT-CSV remove spaces from names?

Thanks


Robert Robelo

unread,
Mar 9, 2010, 12:52:01 AM3/9/10
to
Quoted property names are valid, just remember to retrieve the value from the
elements in the collection.
The easiest way to remove, or replace, spaces from the CSV header is to
separate the header from the data, remove the spaces and set header back.
A more complcated/fun way is to filter the NoteProperty with spaces and
create an AliasProperty for each. Let's do the fun way fist before modifyig
the CSV's content.

# ceate some objects with NoteProperties with spaces in their names
# and export them to a CSV file without type information
1..3 | ForEach-Object {New-Object PSObject -Property @{
'Prop Num 1' = 'Val 1'
'Prop Num 2' = 'Val 2'
'Prop Num 3' = 'Val 3'
}
} | Export-Csv C:\Test.csv -NoTypeInformation

# quoted property name is valid
foreach ($i in Import-Csv C:\Test.csv) {
$i.'Prop Num 2'
}

# collect objects
$col0 = Import-Csv C:\Test.csv
# quoted property name on an element is valid
$col0[1].'Prop Num 1'

# - - - - - - - - - - - - - #

# build the scriptblock's content
$sbText = Import-Csv C:\Test.csv |
# filter NoteProperty with space(s)
Get-Member '* *' -MemberType NoteProperty | ForEach-Object {
# virtual Add-Member command to create the AliasProperty
"Add-Member AliasProperty {0} '{1}' -InputObject `$_" `
-f ($_.Name -replace ' '), $_.Name
}
# append a PassThru switch to last virtual Ad-Member command
$sbText[-1] += ' -PassThru'
# join the virtual commands
$sbText = $sbText -join [Environment]::NewLine
# create a ScriptBlock with the virtual commands
$sb = $ExecutionContext.InvokeCommand.NewScriptBlock($sbText)
# collect the objects with the AliasProperties
$col1 = Import-Csv C:\Test.csv | ForEach-Object $sb
# retrieve NoteProperty value through its Alias
$col1[0].PropNum3

foreach ($i in Import-Csv C:\Test.csv | ForEach-Object $sb) {
$i.PropNum1
}

# - - - - - - - - - - - - - #

# to remove/replace spaces in the CSV header
# separate header from data
$header, $data = ${C:\Test.csv}
# remove spaces
$header = $header -replace ' '
# or replace spaces
# $header = $header -replace ' ', '_'
# then set content back
${C:\Test.csv} = $header, $data

foreach ($i in Import-Csv C:\Test.csv) {
$i.PropNum2
}

$col2 = Import-Csv C:\Test.csv
$col2[1].PropNum1

--
Robert
http://robertrobelo.spaces.live.com/blog/

0 new messages