{"id":3511,"date":"2009-09-13T21:04:19","date_gmt":"2009-09-13T21:04:19","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2009\/09\/13\/select-expandproperty-propertyname\/"},"modified":"2019-02-18T13:12:25","modified_gmt":"2019-02-18T20:12:25","slug":"select-expandproperty-propertyname","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/select-expandproperty-propertyname\/","title":{"rendered":"Select &#8211;ExpandProperty &#060;PropertyName&#062;"},"content":{"rendered":"<p>Most of us are familiar with the traditional use of Select \u2013ExpandProperty &lt;propertyname&gt;.&#160; 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 \u2013PROPERTIES &lt;propertyname[]&gt; parameter.&#160; An example will help:<\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Consolas\">PS&gt; gps |Select -ExpandProperty Modules -ea SilentlyContinue |Group ModuleName |Sort Count |Select -Last 4 <\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Consolas\">Count Name&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Group     <br \/>&#8212;&#8211; &#8212;-&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8212;&#8211;      <br \/>&#160;&#160; 58 kernel32.dll&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {System.Diagnostics.ProcessModule (kernel32&#8230;.      <br \/>&#160;&#160; 58 msvcrt.dll&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {System.Diagnostics.ProcessModule (msvcrt.dl&#8230;      <br \/>&#160;&#160; 58 KERNELBASE.dll&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {System.Diagnostics.ProcessModule (KERNELBAS&#8230;      <br \/>&#160;&#160; 72 ntdll.dll&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {System.Diagnostics.ProcessModule (ntdll.dll&#8230;<\/font><\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>A while ago, Lee Holmes turned me on to another use of \u2013ExpandProperty and I\u2019ve been meaning to blog it ever since.&#160; Have you ever wanted to get just the value for a property from every object?&#160; You\u2019d think you could do this and it would work:<\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Consolas\">PS&gt; gps *ss |select name <\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Consolas\">Name     <br \/>&#8212;-      <br \/>csrss      <br \/>csrss      <br \/>lsass      <br \/>smss<\/font><\/p>\n<p>&#160;<\/p>\n<p>Sadly, this is not what you want. This gives you a set of objects each of which has exactly one property:&#160; NAME.&#160; What you wanted was a stream of VALUES.&#160; What I\u2019ve always ended up doing was this:<\/p>\n<p>&#160;<\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Consolas\">PS&gt; gps *ss | foreach {$_.Name}     <br \/>csrss      <br \/>csrss      <br \/>lsass      <br \/>smss<\/font><\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p>This works, it is not too hard but it always sorta pissed me off that I had to do it this way.&#160; Sadly, addressing this never made it above the cut like for V2.&#160; Again \u2013 to ship is the choose.&#160; 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 \u2013ExpandProperty \u2013 it gives you exactly what you want.<\/p>\n<p><font color=\"#0000ff\" size=\"2\" face=\"Consolas\">PS&gt; gps *ss |select -ExpandProperty name     <br \/>csrss      <br \/>csrss      <br \/>lsass      <br \/>smss<\/font><\/p>\n<p>&#160;<\/p>\n<p>SWEET!<\/p>\n<p>Enjoy! <\/p>\n<p>Jeffrey Snover [MSFT]   <br \/>Distinguished Engineer    <br \/>Visit the Windows PowerShell Team blog at:&#160;&#160;&#160; <a href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/a>    <br \/>Visit the Windows PowerShell ScriptCenter at:&#160; <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Most of us are familiar with the traditional use of Select \u2013ExpandProperty &lt;propertyname&gt;.&#160; 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 \u2013PROPERTIES &lt;propertyname[]&gt; parameter.&#160; An example will help: PS&gt; gps [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3511","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>Most of us are familiar with the traditional use of Select \u2013ExpandProperty &lt;propertyname&gt;.&#160; 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 \u2013PROPERTIES &lt;propertyname[]&gt; parameter.&#160; An example will help: PS&gt; gps [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/3511","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=3511"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/3511\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=3511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=3511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=3511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}