Tuesday, July 7, 2015

Beginner's Guide - Working with lists (part 3)

This is the third installment of my beginner's guide to working with lists.  Part 1 covered simple string arrays and Part 2 dived into object arrays and working with them.  We'll cover building custom object arrays from scratch in this section.

Making Custom Objects

Since we already know a bit about working with arrays in general and we know that object arrays are just ordered collections of objects, we are going to spend most of this covering building custom objects.  There are two ways to make a custom object.

The first way is to define an empty object and then add parameters, along with data types and data, one at a time.  This way is more common because order of the parameters is preserved so when you output your object arrays, they columns (parameters) are in the in the order you'd like and you don't have to explicitly name an order of your preference.

The second way is quicker, as you add all of the parameters, data types, and data at once, but you lose the order in which your parameters are added.  This isn't a problem if you explicitly name your output order with either a select or format-table statement, but you just have to remember to do that.

Lets define some objects using the first way and then add them together into an array.
PS C:\> $person1 = New-Object psobject
PS C:\> Write-Output $person1

PS C:\> $person1 | Add-Member -Type NoteProperty -Name "First" -Value "Bob"
PS C:\> $person1 | Add-Member -Type NoteProperty -Name "Last" -Value "Smith"
PS C:\> $person1 | Add-Member -Type NoteProperty -Name "Age" -Value 40
PS C:\> $person1 | Add-Member -Type NoteProperty -Name "isAlive" -Value $true
PS C:\> $person1 | Add-Member -Type NoteProperty -Name "Birthdate" -Value ([datetime]"4/2/75")
PS C:\> Write-Output $person1


First     : Bob
Last      : Smith
Age       : 40
isAlive   : True
Birthdate : 4/2/1975 12:00:00 AM

Let's make another person object.
PS C:\> $person2 = New-Object psobject
PS C:\> $person2 | Add-Member -Type NoteProperty -Name "First" -Value "Mary"
PS C:\> $person2 | Add-Member -Type NoteProperty -Name "Last" -Value "Jones"
PS C:\> $person2 | Add-Member -Type NoteProperty -Name "Age" -Value 45
PS C:\> $person2 | Add-Member -Type NoteProperty -Name "isAlive" -Value $true
PS C:\> $person2 | Add-Member -Type NoteProperty -Name "Birthdate" -Value ([datetime]"2/21/70")
PS C:\> Write-Output $person2


First     : Mary
Last      : Jones
Age       : 45
isAlive   : True
Birthdate : 2/21/1970 12:00:00 AM
Lets now combine the two objects into a new object array. As we saw in part 1, the notation of using @() will define an empty array.
PS C:\> $people = @($person1,$person2)

PS C:\> $people.GetType()                    #We created a new object array with two records

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

PS C:\> $people.Count
2

PS C:\> $people | Format-Table -AutoSize

First Last  Age isAlive Birthdate
----- ----  --- ------- ---------
Bob   Smith  40    True 4/2/1975 12:00:00 AM
Mary  Jones  45    True 2/21/1970 12:00:00 AM

The second way: Hashtable

As I briefly covered in part 1, hashtables are collections of unordered name & value pairs.  The second way to define a custom object is to pass in a parameter list in the form of a hashtable.
PS C:\> $person3 = New-Object psobject -Property @{"First"="Sandra"; "Last"="Johnson"; "Age"=85; "isAlive"=$false; "Birthdate"=([datetime]"7/1/30")}
PS C:\> Write-Output $person3


Birthdate : 7/1/1930 12:00:00 AM
Last      : Johnson
First     : Sandra
Age       : 85
isAlive   : False
As you can see, I've defined the hashtable as part of the parameter for new-object. Lets create another person.
PS C:\> $HT = @{"First"="James"; "Last"="Brown"; "Age"=95; "isAlive"=$false; "Birthdate"=([datetime]"3/19/20")}
PS C:\> $person4 = New-Object psobject -Property $HT
PS C:\> Write-Output $person4


isAlive   : False
First     : James
Age       : 95
Birthdate : 3/19/2020 12:00:00 AM
Last      : Brown

Again, it's random as to how parameters are added. Now lets combine them along with our first list.
PS C:\> $morepeople = @()            #Since the first object I'm combining is already an object array, I could have skipped this line.
PS C:\> $morepeople += $people       #adds the two records from the first object array
PS C:\> $morepeople += $person3      #the plus-equal operator appends the object array with another record
PS C:\> $morepeople += $person4
PS C:\> $morepeople | Format-Table First,Last,Age,isAlive,Birthdate -AutoSize   #Here is where I explicitly define the order.

First  Last    Age isAlive Birthdate
-----  ----    --- ------- ---------
Bob    Smith    40    True 4/2/1975 12:00:00 AM
Mary   Jones    45    True 2/21/1970 12:00:00 AM
Sandra Johnson  85   False 7/1/1930 12:00:00 AM
James  Brown    95   False 3/19/2020 12:00:00 AM



In part 4, we'll go into more advanced ways of selecting parts of existing object arrays to make new ones using custom expressions.

No comments: