0

I'm trying to create a custom object but receive error. Does anyone have ideas on how to deal with this issue?

$tempObject = New-Object System.Object
Add-Member -inputobject $tempObject -type NoteProperty -name Name -value ""
Add-Member -inputobject $tempObject -type NoteProperty -name account   status -value ""
Add-Member -inputobject $tempObject -type NoteProperty -name OU -value ""
Add-Member -inputobject $tempObject -type NoteProperty -name Last Logon -value ""

Add-Member : The SecondValue parameter is not necessary for a member of type "NoteProperty", and should not be specified. Do not specify the SecondValue parameter when you add members of this type.

shA.t
  • 16,580
  • 5
  • 54
  • 111
andrew_b
  • 29
  • 1
  • 4

1 Answers1

3

Where you have spaces in your strings, put them in quotes (single or double):

$tempObject = New-Object System.Object
Add-Member -inputobject $tempObject -type NoteProperty -name Name -value ""
Add-Member -inputobject $tempObject -type NoteProperty -name 'account   status' -value ""
Add-Member -inputobject $tempObject -type NoteProperty -name OU -value ""
Add-Member -inputobject $tempObject -type NoteProperty -name 'Last Logon' -value ""

Update

Per @wannabeprogrammer's comment below, a shorthand way of achieving the above would be:

$tempObject2 = [PSCustomObject]@{ 
    Name = "" ; 
    'Account Status' = "" ; 
    OU = "" ; 
    'Last Logon' = "" 
}

...or to get exactly the same (i.e. so a compare-object of the two methods shows no difference between the objects being created).

$tempObject3 = New-Object -TypeName PSObject -Property @{ 
    Name = "" ; 
    'Account Status' = "" ; 
    OU = "" ; 
    'Last Logon' = "" 
}

See what I mean by running the above snippets, then the below:

compare-object $tempObject $tempObject2
compare-object $tempObject $tempObject3
compare-object $tempObject2 $tempObject3
JohnLBevan
  • 22,735
  • 13
  • 96
  • 178
  • 2
    just a remark, you can create custom object faster by casting a hash to PSCustomObject, e.g. `[PSCustomObject]@{ Name = "" ; 'Account Status' = "" ; OU = "" ; 'Last Logon' = "" }` – tkokasih Feb 11 '15 at 03:28