Prof. Powershell

Check Out the -View!

The Get-Member cmdlet's -View parameter gives you -- you guessed it -- a wider perspective of the cmdlet's members.

I've written many times about the object-ness of Windows PowerShell, which is what makes it easy to accomplish a lot with very little effort. In the past I've mentioned using the Get-Member cmdlet to discover an object's properties and methods:

PS C:\> get-wmiobject win32_logicaldisk | get-member

If you run this command you will see all the object's members -- that is, its properties and methods for the WMI Win32_LogicalDisk class. But the truth is a little more complicated than that.

Because PowerShell is aimed at system administrators who are not required to be .NET developers, the Windows PowerShell team went to great lengths to make sure objects are easy to work and meaningful. In many cases this meant adapting or extending an object to offer additional functionality. When you pipe an object to Get-Member, you see most of their efforts. But advanced PowerShell admins might want to dive a little deeper.

The Get-Member cmdlet has a parameter called -View, which allows you, as the name suggests, to view the object's members from different perspectives:

PS C:\> help get-member -Parameter View

-View <PSMemberViewTypes>
Gets only particular types of members (properties and methods). Specify one or more of the values. The default is "Adapted, Extended".

Valid values are:
-- Base: Gets only the original properties and methods of the .NET Framework object (without extension or adaptation).

-- Adapted: Gets only the properties and methods defined in the Windows
PowerShell extended type system.

-- Extended: Gets only the properties and methods that were added in the
Types.ps1xml files or by using the Add-Member cmdlet.

-- All: Gets the members in the Base, Adapted, and Extended views.

The View parameter determines the members retrieved, not just the display of
those members.

To get particular member types, such as script properties, use the MemberType
parameter. If you use the MemberType and View parameters in the same command, Get-Member gets the members that belong to both sets. If you use the Static and View parameters in the same command, the View parameter is ignored:

Required?                   false
Position?                   named
Default value
Accept pipeline input?      false
Accept wildcard characters? false

Let's see what this means and return to our Win32_LogicalDisk object. Try this:

PS C:\> get-wmiobject win32_logicaldisk | get-member -view base

The Base view is the raw .NET definition of the object. Sometimes the object definition needs a little help to make it easier to use. For example, in the base object we just looked at there is a property called SystemProperties, but it is awkward to use. In many objects, the PowerShell team added adaptations:

PS C:\> get-wmiobject win32_logicaldisk | get-member -view adapted

The results you see here are defined in the XML type configuration files you'll find in $PSHOME. All of the properties from SystemProperties have been “expanded” and now the object is easier to use. But the PowerShell team didn't stop there. Often you will find extended functionality. These are object members that are defined, sometimes calculated, to make your life even easier. Take a look at the extended properties of the Win32_LogicalDisk object:

PS C:\> get-wmiobject win32_logicaldisk | get-member -view extended

These properties are also defined in the XML configuration files but make it easier to write meaningful expressions.

PS C:\> get-wmiobject win32_logicaldisk | select PSStatus

Your results might vary, but the PSStatus property only exists within PowerShell.

It is possible to add your own type extensions in PowerShell, but that is a topic for a future lesson. Or check out Windows PowerShell 2.0: TFM (SAPIEN Press 2010) for more detailed instructions.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular