In PowerShell version 2, it took me a while to figure out why using the dotted notation method of selecting a property worked at times and not at others.
In the following example $v2 is a variable so I’m able to select the CPU property by using $v2.cpu or accomplish the same thing by piping $v2 to Select-Object with the -ExpandProperty parameter and specifying the CPU property:
In the scenario shown in the following image, why didn’t $v2.cpu return anything? Because it’s a collection. In this example, $v2 contains a collection of items (objects) which is why $v2.cpu doesn’t return anything in PowerShell version 2. Piping $v2 to Select-Object as in the previous example returns all of the items in the collection for the CPU property:
Collections are zero based and each individual item in it can be accessed by specifying the position of the item you want to retrieve. The number of items that a collection contains can be determined by retrieving the length property:
Piping the collection to Select-Object or ForEach-Object are ways to retrieve all of the items in the collection:
In PowerShell version 3, using the dotted notation method of accessing a property that’s part of a collection will return all of the items in the collection unlike in PowerShell version 2. An example is shown in the following image where $v3.processname returns all of the ProcessName items from the $v3 collection without having to pipe it to Select-Object or ForEach-Object as would have been necessary in PowerShell version 2:
Now for one last screenshot to make sure that what’s happening is clear. The same commands are run in PowerShell version 2 and 3 and they produce different results:
As you can see, when working with a collection in PowerShell version 3, it’s more transparent than version 2 because you don’t even need to know you’re working with a collection to return items using the dotted notation method of selecting a property.