Why PS Custom Object?

This topic contains 4 replies, has 4 voices, and was last updated by  Rob Simmers 1 month ago.

  • Author
    Posts
  • #102631

    Sankhadip Roy
    Participant

    Hi PowerGuyz,
    I have 2 questions for clearing my doubt. Can you please help me out.

    I just learned how to create PS custom object. But can't understand why to create? In my perspective, I can use HashTable, array etc instead of a custom object. But what will be the benefit if I create custom objects? I am googling it from yesterday, but didn't get any proper answer. Can you please clear me the concept?

    My Second question is How to add values to hashtable using loop? For array we can use $array+= option. Is there anything for hashtable?

    Thanks in advance.

    Regards,
    Roy.

  • #102635

    James Crompton
    Participant

    Hi Sankhadip,

    As for your first question, PSCustom objects have many features which hashtables do not have. Before talking about the features, there are also some functional "gotchas" when trying to use just hashtables rather than PSCustom objects for your generic objects. Here is an example:

    $hashTableObject = @{}
    
    $hashTableObject.Name = "Name"
    $hashTableObject.Location = "Home"
    
    $hashTableObject # This prints showing the values as expected.
    $hashTableObject | ft Name, Location # This does not show the columns as expected. Someone with an "object" would expect to see the name and location property values printed.
    
    $psObject = [PSCustomObject] @{Name="Name";Location="Home"}
    $psObject | ft Name, Location #prints as expected.
    

    In addition to avoiding issues like above where the hashtable object does not behave as one might expect, PSCustomobjects provides you with the ability to associate TypeNames and default display properties to the object. This lets you do many things, the most common of which is to control which properties of the object are displayed by default when you print it to the screen. This can be very useful for giving a friendly view of an object while still having many additional, properties available off the object which are not usually read by a human. You can also do many other things once an object has a typename (TypeNames.Insert(0, "MyType")) see https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_types.ps1xml?view=powershell-6 for more information.

    As to your second question, you can add to a hashtable simply by adding a key:

    $myHashTable["Key"] = $value
    
    #for example:
    $myHashTable = @{}
    1..10 | % { $myHashTable[$_] = "Test$_"} # loop through numbers 1 to 10 and add 10 keys to the hashtable.
    $myHashTable # contents are 10 items with the value Test(Num)
    
    • #102638

      Sankhadip Roy
      Participant

      Great Man... Thanks for the example. Got it now...
      We should use Custom Object. 🙂

  • #102647

    js
    Participant

    Objects are the basic unit of communication between cmdlets in powershell. If you're lucky, some properties have the same name between cmdlets in the pipeline.

  • #102697

    Rob Simmers
    Participant

    Would like to add one important reason that PSObject should be leveraged. It is how you leverage the pipeline in Powershell. Most cmdlets will produce a PSObject and if the accept pipeline process and parse that object. Think of it as the shared media that Powershell understands how to parse and use. An analogy would be if you asked me for a movie and I handed you a USB drive with some obscure format, you could do extra work to convert that movie into a standard format to make a DVD (like a hashtable). The other choice is I could give you the movie as a DVD (PSObject) and you could play it on any DVD player (cmdlet) in hundreds of devices anywhere. It is Powershell's default format.

    Just one last thing, a PSObject is basically an array @() of hash tables @{}.

You must be logged in to reply to this topic.