concept member enumeration in category powershell

appears as: member enumeration
PowerShell Deep Dives

This is an excerpt from Manning's book PowerShell Deep Dives.

Before member enumeration you had to do some fancy tricks to handle this. PowerShell now does it for you under the hood. Member enumeration also enumerates, among other things, all possible properties and methods for these objects, which you can use.

You can use member enumeration to get all the values of a single property on one or a bunch of objects, like so:

PS C:\> (Get-Process).Name

Name
----
armsvc
conhost
csrss
csrss
dasHost
dwm
explorer
...

Note that member enumeration works only with a single property. To select multiple properties use the Select-Object cmdlet in the pipeline.

I didn’t provide the execution time of this script because it can be misleading. In this example I’m getting the names of the processes, and using member enumeration takes more time than using the pipeline.

The gotcha with member enumeration is that the more objects you handle, the faster it gets relative to using the pipeline. For example, on a disk with many files and directories, time the execution of the following two commands:

Get-ChildItem –Recurse | Select-Object –Property Name
(Get-ChildItem).Name

You’ll find that member enumeration yields the shorter time.

Let’s look at another example of member enumeration. This time we’ll use it with a method instead of a property.