Powershell: refers to a property containing a space

I am importing a CSV file using powershell.

The problem is that the heading of one of the columns is "Remaining time - hours". Therefore, I get a sequence of objects, and in fact it is assigned the property "Remaining time - hours".

What is the syntax for referencing this property?

eg,

Import-Csv AllOpenCases.csv | % {$case = $_ } $case | get-member 

returns

 Category : Inquiry Starred : No Remaining Time - Hours : 22.5 

but if I print

 $case.Remaining Time - Hours 

I get an "Unexpected token" Time "in the expression or expression"

+42
powershell csv
Feb 01 2018-11-11T00:
source share
4 answers

Properties with embedded spaces must be specified when referencing:

  $case."Remaining Time - Hours" 
+74
Feb 01 2018-11-11T00:
source share

Just to add, the property name can be a variable, for example:

 PS> $o = new-object psobject -prop @{'first name' = 'John'; 'last name' = 'Doe'} PS> $o last name first name --------- ---------- Doe John PS> $prop = 'first name' PS> $o.$prop John 
+20
Feb 01 '11 at 6:29
source share

Or you can also wrap this property in {}. Like this:

  $case.{Remaining Time - Hours} 
+14
Feb 01 2018-11-11T00:
source share

FWIW, if it hurts you to write code, you can add the alias property:

  $caseprops = @" Category = Inquiry Starred = No Remaining Time - Hours = 22.5 "@ $case = new-object psobject -property ($caseprops | convertfrom-stringdata) $case | add-member aliasproperty "RT" "Remaining Time - Hours" $case | fl Remaining Time - Hours : 22.5 Starred : No Category : Inquiry RT : 22.5 
+9
Feb 01 '11 at 12:44
source share



All Articles