Get-PropertyValue in PowerShell Community Extensions

Get-PropertyValue, alias gpv, is an easy way to get and/or display just the specified property of an object in the pipeline.  Where this really comes in handy is when you are in discovery mode and just want to figure out how to solve a problem.  For example, let’s say you are learning about the Get-Process cmdlet and the Process objects returned by it.  You know about type member discovery using Get-Member so you try this:
 
> Get-Process | Get-Member
 
   TypeName: System.Diagnostics.Process
Name                           MemberType     Definition
—-                           ———-     ———-
Handles                        AliasProperty  Handles = Handlecount
Name                           AliasProperty  Name = ProcessName
NPM                            AliasProperty  NPM = NonpagedSystemMemorySize
PM                             AliasProperty  PM = PagedMemorySize
VM                             AliasProperty  VM = VirtualMemorySize
WS                             AliasProperty  WS = WorkingSet
add_Disposed                   Method         System.Void add_Disposed(EventHandler value)
<snip>
StartInfo                      Property       System.Diagnostics.ProcessStartInfo StartInfo {get;set;}
<snip>
 
You spot an interesting property called StartInfo that you want to examine.  You might think that you could do this:
 
> Get-Process | Select StartInfo | Get-Member
 
but that would return info on a PSCustomObject e.g.:
 
   TypeName: System.Management.Automation.PSCustomObject
Name        MemberType   Definition
—-        ———-   ———-
Equals      Method       System.Boolean Equals(Object obj)
GetHashCode Method       System.Int32 GetHashCode()
GetType     Method       System.Type GetType()
ToString    Method       System.String ToString()
StartInfo   NoteProperty System.Diagnostics.ProcessStartInfo StartInfo=System.Diagnostics.ProcessStartInfo
 
That is consistent with the intent of the Select-Object cmdlet which is to create a new object that is just a container object for the properties that we selected.  So how do we get info on this StartInfo property?  We could also do this:
 
> 19> (Get-Process)[0].StartInfo | Get-Member
   TypeName: System.Diagnostics.ProcessStartInfo
<snip>
 
This works OK but isn’t exactly obvious.  Then next approach suffers the same (isn’t obvious):
 
> Get-Process | Get-Member -InputObject {$_.startinfo}
 
With PSCX installed, you can do this a bit more easily:
 
> Get-Process | gpv StartInfo | Get-Member
Advertisements
This entry was posted in PSCX. Bookmark the permalink.

3 Responses to Get-PropertyValue in PowerShell Community Extensions

  1. Jeffrey says:

    Should we incorporate this function into Get-Member?  If so, what would you like to type in order to get this (e.g. what should the parameter syntax be)?
     
    Jeffrey Snover [MSFT]Windows PowerShell/MMC ArchitectVisit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShellVisit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

  2. Oisin says:

    What about -InputProperty? or perhaps -FromProperty is more intuitive?
     
    PS > Get-Process | Get-Member -FromProperty StartInfo
     
    – Oisin
    http://wwww.codeplex.com/PowerShellCXhttp://www.codeplex.com/PSSharePoint

  3. Keith says:

    I think we could live without this but if you put something in to handle this use case then I would suggest one of the following:
     
    PS> Get-Process | Get-Member -OfProperty StartInfo

    PS> Get-Process | Get-Member -Property StartInfo
     
    The other object is to create a cmdlet like the Get-PropertyValue filter in PSCX.  It is useful for other scenarios like a lot of cases where you would otherwise have to bounce around the command line to place parens around an expression followed by ".<propertyname>".

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s