Exploiting Select-Object

by Feb 13, 2018

Select-Object is a basic cmdlet that most PowerShell users use frequently. However, it has some tricks that are not well known.

In its most basic form, Select-Object selects the properties that are visible. If you don’t use Select-Object, then PowerShell determines which properties are shown, and how they are formatted:

Get-ChildItem -Path c:\windows\system32 -Filter *.dll

If you add Select-Object, you can determine for yourself which properties are visible, for example:

Get-ChildItem -Path c:\windows\system32 -Filter *.dll |
  Select-Object -Property CreationTime, Length, Name, VersionInfo

Select-Object can also move property content one level up. In the previous example, VersionInfo contained another object. By using the -ExpandProperty parameter, you can move its properties up one level:

Get-ChildItem -Path c:\windows\system32 -Filter *.dll |
  Select-Object -Property CreationTime, Length, Name -ExpandProperty VersionInfo

To actually see the merged properties, send the result to Select-Object again, because else PowerShell would only show some of them:

Get-ChildItem -Path c:\windows\system32 -Filter *.dll |
  Select-Object -Property CreationTime, Length, Name -ExpandProperty VersionInfo |
  Select-Object -Property *

Instead of “*”, you can now use a comma-separated list of properties you’d like to see:

Get-ChildItem -Path c:\windows\system32 -Filter *.dll |
  Select-Object -Property CreationTime, Length, Name -ExpandProperty VersionInfo |
  Select-Object -Property CreationTime, Name, FileVersionRaw, CompanyName

Are you an experienced professional PowerShell user? Then learning from default course work isn’t your thing. Consider learning the tricks of the trade from one another! Meet the most creative and sophisticated fellow PowerShellers, along with Microsoft PowerShell team members and PowerShell inventor Jeffrey Snover. Attend this years’ PowerShell Conference EU, taking place April 17-20 in Hanover, Germany, for the leading edge. 35 international top speakers, 80 sessions, and security workshops are waiting for you, including two exciting evening events. The conference is limited to 300 delegates. More details at www.psconf.eu.

Twitter This Tip! ReTweet this Tip!