Welcome to MSDN Blogs Sign in | Join | Help

Select –ExpandProperty <PropertyName>

Most of us are familiar with the traditional use of Select –ExpandProperty <propertyname>.  This takes a the value of an incoming object, enumerates its values and outputs each of those values as a single record on the output stream after adding any properties specified by the –PROPERTIES <propertyname[]> parameter.  An example will help:

PS> gps |Select -ExpandProperty Modules -ea SilentlyContinue |Group ModuleName |Sort Count |Select -Last 4

Count Name                      Group
----- ----                      -----
   58 kernel32.dll              {System.Diagnostics.ProcessModule (kernel32....
   58 msvcrt.dll                {System.Diagnostics.ProcessModule (msvcrt.dl...
   58 KERNELBASE.dll            {System.Diagnostics.ProcessModule (KERNELBAS...
   72 ntdll.dll                 {System.Diagnostics.ProcessModule (ntdll.dll...

 

 

A while ago, Lee Holmes turned me on to another use of –ExpandProperty and I’ve been meaning to blog it ever since.  Have you ever wanted to get just the value for a property from every object?  You’d think you could do this and it would work:

PS> gps *ss |select name

Name
----
csrss
csrss
lsass
smss

 

Sadly, this is not what you want. This gives you a set of objects each of which has exactly one property:  NAME.  What you wanted was a stream of VALUES.  What I’ve always ended up doing was this:

 

PS> gps *ss | foreach {$_.Name}
csrss
csrss
lsass
smss

 

 

This works, it is not too hard but it always sorta pissed me off that I had to do it this way.  Sadly, addressing this never made it above the cut like for V2.  Again – to ship is the choose.  I was complaining about this one day and Lee pointed out that if you specify a scalar property (a property that is NOT a collection) for –ExpandProperty – it gives you exactly what you want.

PS> gps *ss |select -ExpandProperty name
csrss
csrss
lsass
smss

 

SWEET!

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

Published Monday, September 14, 2009 2:04 AM by PowerShellTeam

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Select –ExpandProperty <PropertyName>

Awesome, thanks Jeffrey!  

I always look forward to these posts and am working to drive PowerShell adoption at my company.   Your prior post on "Quick and Dirty Scripting" was right on.  I spend my day trying to make the shortest most flexible (and valuable) scripts to keep my admin time minimal.   We all appreciate your hard work to automate admin work here.

Monday, September 14, 2009 9:16 AM by Jeff Jones

# re: Select –ExpandProperty <PropertyName>

Haha.  I always hate having the foreach loop too.   You guys have too many features in Powershell that I find out on this blog and not while using it :)

Thanks for the post.

Monday, September 14, 2009 10:29 AM by Brian Hartsock

# re: Select –ExpandProperty <PropertyName>

Why is this an improvement? It's more characters to type (than %{$_.Name}).

Monday, September 14, 2009 11:33 AM by Dave

# re: Select –ExpandProperty <PropertyName>

@Dave - because of tab-completion

jps

Monday, September 14, 2009 6:11 PM by PowerShellTeam

# re: Select –ExpandProperty <PropertyName>

When I try that final command, I get this error:

Select-Object : Cannot expand property "name" because it has nothing to expand.

At line:1 char:16

+ gps *ss |select  <<<< -ExpandProperty name

Thursday, September 17, 2009 5:34 AM by Serfmum Yossarian

# re: Select –ExpandProperty <PropertyName>

That sounds like the TypeExpansion files didn't load during startup.  Try using Processname instead.

Then figure out why you are missing the type expansion stuff.  make sure the file exists:

test-path $pshome/types.ps1xml

then update the data:

Update-TypeData $pshome/types.ps1xml

and try it again with "NAME".

jps

Thursday, September 17, 2009 8:39 AM by PowerShellTeam

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker