PSStandardMembers – The Stealth Property

PowerShell Team

Today I got the question

I’ve looked into all of my.format.ps1xml files but I can’t find out where Format-List is getting its default view for, say, System.Diagnostics.Process:

PS> ps |select -first 1 |fl

Id      : 7616
Handles : 543
CPU     : 10.6860685
Name    : CmdletDesigner

How does it know it should display just these four properties?
It seems there isn’t any ListControl view selected by this type within those formatting files…

Excellent question!  Let’s pick behind the curtain and see how it works.  When an object gets to one of our formatting commands, we use its type to look and see if there is a VIEW defined for it.  These views are defined in our formatting files in $PSHOME directory.  They have filenames like xxx.Format.ps1xml .  If we find it, we use it.  If we don’t find it, we look to see whether this type has a DefaultDisplayPropertySet defined.  If it does, we use it.  If it doesn’t we emit all the properties either as a table or a list depending upon how many properties there are.

 

But wait a minute – let’s go back and look at this DefaultDisplayPropertySet – what’s that all about?  If you’ve been using PowerShell a while, you’ll realize that PowerShell uses a type mashup system.  Mashups are really just an identity space and a mechanism for 3rd parties to add addition information into an identity space.  For map mashups, the identity space is latitude/longitude and a mechanism for someone to provide the lat/long for all the Starbucks in the world.  For PowerShell, we use TYPEs as an identity space and then have various mechanisms for 3rd parties to add extra information.  One of those mechanisms is TYPE files (e.g. $pshome/types.ps1xml ).  If you take a look at the file, you’ll see that we have a set of entries which are keyed off of a typename and can add extra information.  If you search for System.Diagnostics.Process – you’ll see a bunch of stuff.   One of the things you can add to a type is a MemberSet

 

One of the things you’ll see is a super double secret MemberSet called PSStandardMembers.   This is a hidden memberset that PowerShell uses for it’s own purposes.  In that memberset, you (we) can define something called DefaultDisplayPropertertySet which provides a set of properties that should be displayed if there isn’t a view defined.  Let me show you that it is really there (you can follow along at home):

PS> $x = Get-Process |Select -First 1
PS> $x |Format-List

Id      : 7616
Handles : 543
CPU     : 10.7172687
Name    : CmdletDesigner

PS> # Let me prove to you that it is hidden
PS> $x |Get-Member PSStandardMembers


PS> # It’s really there, just hidden.  You can show hidden things using -Force
PS> $x |Get-Member PSStandardMembers -Force

   TypeName: System.Diagnostics.Process

Name              MemberType Definition
—-              ———- ———-
PSStandardMembers MemberSet  PSStandardMembers {DefaultDisplayPropertySet}

PS> $x.PSStandardMembers
PSStandardMembers {DefaultDisplayPropertySet}


PS> $x.PSStandardMembers.DefaultDisplayPropertySet

ReferencedPropertyNames : {Id, Handles, CPU, Name}
MemberType              : PropertySet
Value                   : DefaultDisplayPropertySet {Id, Handles, CPU, Name}
TypeNameOfValue         : System.Management.Automation.PSPropertySet
Name                    : DefaultDisplayPropertySet
IsInstance              : False

PS> $x.PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames
Id
Handles
CPU
Name
PS>

 

Now it really isn’t secret we just haven’t talked about it much.  It is documented @ MSDN HERE .

PSStandardMembers is used for a number of things in addition to DefaultDisplayProperties.  In particular, it is used to control object serialization.  You can find out more about that in my blog entry HERE.

 

Enjoy!

Jeffrey Snover [MSFT]
Distinguished Engineer
Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

0 comments

Discussion is closed.

Feedback usabilla icon