PowerShell Objects Part 2: Do It Yourself

In part one of our discussion concerning PowerShell objects we discussed the integration of .NET into PowerShell, how this has brought us to the object-oriented paradigm that it utilizes and how to work with these objects. Now, as an extension of that, we will be using objects ourselves when we are scripting. This is when it gets fun.

Let’s suppose I have an XML file that contains weightlifting data on a number of collegiate athletes. As a simple example we’ll use the following format (There may be any number of <Athlete>s):

<Athlete1RMData>
    <Athlete LastName ="">
            <BenchPress></BenchPress>
            <MilitaryPress></MilitaryPress>
            <Squat></Squat>
    </Athlete>
    <Athlete LastName ="">
            <BenchPress></BenchPress>
            <MilitaryPress></MilitaryPress>
            <Squat></Squat>
    </Athlete>
</Athlete1RMData>

The first thing to be aware of is that PowerShell is perfectly capable of importing this XML file and will parse it into an object for you to manipulate. This can also be done with CSV files.

$AthleteData = [[xml]](Get-Content C:\Path\to\XML\file.xml)

$AthleteData is now a .Net object (System.Xml.XmlNode) with properties and sub-properties. For example, I can return the 1-Rep Max values for Bench Press for all athletes by:

$AthleteData.Athlete1rmData.Athlete.BenchPress

Having all of this data in a single object isn’t very convenient for future processing, so let’s create some objects that will be easier to work with. I will create an arraylist of athletes, each of which will have a last name and all three 1RM stats associated with him. We do this by creating an arraylist object, iterating through the XML object, and creating objects for individual athletes. Then we add attributes:

$Roster = New-Object System.Collections.Arraylist
foreach ($Athlete in $AthleteData.Athlete1RMData.Athlete)
{
  #Creates a new object that represents an athlete
  $AthObject = New-Object System.Object    

  #Adds properties to the object
  $AthObject | Add-Member -type NoteProperty -name LastName -value $Athlete.LastName
  $AthObject | Add-Member -type NoteProperty -name 1RMBench -value $Athlete.BenchPress
  $AthObject | Add-Member -type NoteProperty -name 1RMMilitary -value $Athlete.MilitaryPress
  $AthObject | Add-Member -type NoteProperty -name 1RMSquat -value $Athlete.Squat

  #Adds the object to our list
  $Roster.Add($AthObject)
}

Look at what we’ve done. Once we create a list, we iterate through all of the athletes represented in the XML file using a foreach loop. Then, for each athlete we create an object representing an individual. Finally we begin adding values that we extract from the XML as properties into our new athlete object. Each of these properties has a name associated with it which we can use to make reference to its corresponding value later on. Visually this is what we’ve created:
ObjectsPart2
Now that we have our athletes in the form of .Net objects we can more easily do work with them. We can add additional athlete information, make comparisons, find averages, format/export data and a whole host of useful operations.