Check Out the -View!
The Get-Member cmdlet's -View parameter gives you -- you guessed it -- a wider perspective of the cmdlet's members.
- By Jeffery Hicks
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
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
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:
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.
Jeffery Hicks is a Microsoft MVP in Windows PowerShell, Microsoft Certified Trainer and an IT veteran with over 20 years of experience, much of it spent as an IT consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He works today as an independent author, trainer and consultant. Jeff writes the popular Prof. PowerShell column for MPCMag.com and is a regular contributor to the Petri IT Knowledgebase and 4SysOps. If he isn't writing, then he's most likely recording training videos for companies like TrainSignal or hanging out in the forums at PowerShell.org.
Jeff's latest books are Learn PowerShell 3 in a Month of Lunches, Learn PowerShell Toolmaking in a Month of Lunches and PowerShell in Depth: An Administrators Guide.
You can keep up with Jeff at his blog http://jdhitsolutions.com/blog, on Twitter at twitter.com/jeffhicks and on Google Plus (http:/gplus.to/JeffHicks)