none
Add an object as a property of a custom object RRS feed

  • Question

  • Hello!

    This link contains a very useful example about the creation of custom objects. A string can easily be added as a property of a new, custom object as showed in this line:

    $objAverage | Add-Member -type NoteProperty -name Name -value $objBatter.Name

    $objAverage is the object just created.

    Is it possible to add an entire object as a property of the new, custom object? Which should be the syntax?

    I tried something similar to

    $objAverage | Add-Member -type NoteProperty -name Name -value $objBatter
    and it didn't work.

    I don't need a simple array of objects, like in this case. I would rather like to have an object capable to store other objects inside, together with strings and other data.
    • Edited by Henry_8198 Monday, February 23, 2015 5:57 PM Added clarification.
    Monday, February 23, 2015 5:49 PM

Answers

  • NoteProperty is implicitly a [string], so it works for adding a string property, but not an [object].

    What version are you running?  If you've got at least V3 you can do that like this:

    $object1 = [pscustomobject]@{name='Rob'}
    $object2 = [pscustomobject]@{object=$object1}
    
    $object2.object
    
    
    name                                                                                                      
    ----                                                                                                      
    Rob            


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    • Marked as answer by Henry_8198 Monday, February 23, 2015 9:20 PM
    Monday, February 23, 2015 7:15 PM
    Moderator

All replies

  • NoteProperty is implicitly a [string], so it works for adding a string property, but not an [object].

    What version are you running?  If you've got at least V3 you can do that like this:

    $object1 = [pscustomobject]@{name='Rob'}
    $object2 = [pscustomobject]@{object=$object1}
    
    $object2.object
    
    
    name                                                                                                      
    ----                                                                                                      
    Rob            


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    • Marked as answer by Henry_8198 Monday, February 23, 2015 9:20 PM
    Monday, February 23, 2015 7:15 PM
    Moderator
  • I am using V4 and your example works. Thank you!

    Add-Member can now be used to add a string property or something else. But can I use the same syntax to add a string to $object2 in addition to $object1?

    I tried both

    $object2 = [pscustomobject]@{object=$object1}@{title='thetitle'}
    $object2 = [pscustomobject]@{object=$object1,title='thetitle"}
    but they don't work.




    • Edited by Henry_8198 Monday, February 23, 2015 9:21 PM
    Monday, February 23, 2015 9:20 PM