Share via


PowerShell TIP: create generic objects to collect and display data


Introduction

Sometimes, there may be a need to collect similar data from different sources and then display that day in a simple way. This can be accomplished using generic, custom objects. This posting walks through the steps for building a simple custom collection object that displays a listing of objects and their properties.

Procedure

  1. Create an empty collection object. Creating a simple system.object is not enough. This type of object cannot contain a suite of objects: it can only contain a single object. What is needed is an object that can in turn contain other objects. For this, use an instance of the System.Collections.ArrayList class, declared like so:

    $MyCollection = New-Object System.Collections.ArrayList

    Leaving off the parentheses creates an empty collection. Here's a short hand way of declaring the same thing:

    $MyCollection = @()

    To declare a finite collection object, use the ArrayList constructor like so:

    $MyCollection = New-Object System.Collections.ArrayList(N)

    where N is some integer. Or it's possible to rely upon the default value of the constructor to create a typed array, like so:

    $MyCollection = new-object system.object[] N

    where N is again any integer. Note: this method works for the other types as well (cfi, system.boolean).

  2. Create an object with the members you want. next task is to create an object that contains the properties and values you want to save. Another name for this is Key:Value pairs. There are several methods for creating such an object. One method is to create a standard system.object and then add members to it. This is the formal textbook script for doing this:

    $MyCollection1 = New-Object System.Object
    $MyCollection1 | Add-Member -type "NoteProperty" -name "Property Name" -value "This is property #1"
    $MyCollection1 | Add-Member -type "NoteProperty" -name "Property Value" -value "Red"
    $MyCollection1 | Add-Member -type "NoteProperty" -name "Property Comment" -value "Always used"
    

    To do a quick check on the values of this object, just use:

    $MyCollection1 | ft -auto

    This is what would be seen in the command shell:

    Let's create a few more of these objects. Each one must be created uniquely:

    $MyCollection2 = New-Object System.Object
    $MyCollection2 | Add-Member -type "NoteProperty" -name "Property Name" -value "This is property #2"
    $MyCollection2 | Add-Member -type NoteProperty -name "Property Value" -value "Blue"
    $MyCollection2 | Add-Member -type NoteProperty -name "Property Comment" -value "Sometimes used"
    $MyCollection2 | ft -auto
     
    $MyCollection3 = New-Object System.Object
    $MyCollection3 | Add-Member -type NoteProperty -name "Property Name" -value "This is property #3"
    $MyCollection3 | Add-Member -type NoteProperty -name "Property Value" -value "yellow"
    $MyCollection3 | Add-Member -type NoteProperty -name "Property Comment" -value "Never used"
    $MyCollection3 | ft -auto
    

    After executing the above script, something like this would be seen in the command shell:

    Let's create a fresh instance of the collection object, and then add to this collection the three objects just created, like so (using the same command shell):

    $MyCollection = New-Object System.Collections.ArrayList
    $MyCollection = $MyCollection + $MyCollection1
    $MyCollection = $MyCollection + $MyCollection2
    $MyCollection = $MyCollection + $MyCollection3
    

    Or, in shorthand:

    $MyCollection = @()
    $MyCollection += $MyCollection1
    $MyCollection += $MyCollection2
    $MyCollection += $MyCollection3
    

    The command shell will now display this:

  3. Retrieve object values. The objects are added to the collection and are indexed in the order in which they were added. To list the contents of the collection, simply enter the collection name itself and pipe it into the Format-Table object, like so:

    $MyCollection | Format-Table -auto

    which will be displayed in the shell as:

    Once the data has been added to a collection object, use standard collection indexing and object notation to retrieve collection values. For example, to retrieve the first object that was added to the collection, just enter:

    $MyCollection[0]

    To retrieve the value of the Property Name field for this object, just enter:

    $MyCollection[0]."Property Name"

    Quotation marks had to be used in this instance due to the field name containing two words. Anyway, this works for all the other object fields as well:

    Here's what it would look like if single-word property names had been used:

    Note how easy it is then to access specific object member values.

References

  1. MSDN: Object Class
  2. MSDN: ArrayList Class
  3. MSDN: Boolean Structure
  4. SS64: Add-Member
  5. Windows PowerShell Tip of the Week: Working With Custom Objects
  6. Windows PowerShell Tip of the Week: Even More Things You Can Do With Arrays
  7. Windows PowerShell Tip of the Week: Working with Hash Tables
  8. C# array of different objects
  9. Windows PowerShell Blog: New-Object PSObject –Property [HashTable]