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.

2
  • There's a space in Last Logon; try instead 'Last Logon' Commented Feb 11, 2015 at 1:39
  • (account status too) Commented Feb 11, 2015 at 1:41

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

just a remark, you can create custom object faster by casting a hash to PSCustomObject, e.g. [PSCustomObject]@{ Name = "" ; 'Account Status' = "" ; OU = "" ; 'Last Logon' = "" }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.