Prof. Powershell


To minimize the visual clutter, the Select-Object cmdlet lets you view only those properties that you're interested in viewing.

Because PowerShell is object-oriented, sometimes you can end up with more information than you need. Some properties have many, many properties but you may only be interested in a few. This problem is easily remedied with the Select-Object cmdlet, which has an alias of Select. All you need to do is specify a comma-separated list of object properties:

PS C:\> dir c:\test\services.xml | Select Fullname,Attributes,Length,CreationTime,LastWriteTime

FullName : C:\test\services.xml
Attributes : Archive
Length : 1502334
CreationTime : 3/17/2008 6:33:04 PM
LastWriteTime : 3/17/2008 6:33:07 PM

When you use Select-Object, it creates a new custom object that copies the original property values and then discards the original object. You can see this for yourself by typing this:

PS C:\> dir c:\test\services.xml | Select Fullname,Attributes,Length,CreationTime,LastWriteTime | get-member

  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()
Attributes    NoteProperty System.IO.FileAttributes
CreationTime  NoteProperty System.DateTime
                           CreationTime=3/17/2008 6:33:04 PM
FullName      NoteProperty System.String
LastWriteTime NoteProperty System.DateTime
                           LastWriteTime=3/17/2008 6:33:07 PM
Length        NoteProperty System.Int64 Length=1502334

All this means is that you can't pipe to another cmdlet that is expecting to do something with a file object. However, the cmdlet is still writing to the pipeline, so you can send the results to cmdlets like Out-File, Export-CSV or ConvertTo-HTML.

One way I use Select-Object is with its -first parameter. This parameter will select the first X number of objects that come through the pipeline:

PS C:\> get-process | sort workingset -desc | select -first 10

Handles NPM(K)  PM(K)  WS(K) VM(M)  CPU(s)     Id ProcessName
------- ------  -----  ----- -----  ------     -- -----------
   2080     37 107352  85184  425   529.31   5384 PrimalScript
    195      9  73476  83744  168   801.94   1600 dwm
    642     17  70004  72544  274    17.22  15488 powershell
    689     25  82756  66716  366   173.07   7252 WINWORD
   1562     47  63664  58812  299   406.62   2252 explorer
   1113     22  69760  44808  280    55.80   1284 svchost
    899     14  47868  39480  168    82.29 5880 SearchIndexer
     49      4  49248  27348   91    78.08   1304 avgrssvc
    327     11  36840  26968  111   304.83   1108 svchost
    635     13  19604  22708  122 2,107.99   3264 sidebar

In this expression I've sorted the output of Get-Process by the workingset property in descending order. These objects are then piped to Select-Object, which selects the first 10. There is also a -last parameter that works the same way.

Even though I've been showing you how to be picky, there may be times when you want to see all the available properties for a given object. Use an expression like this:

PS C:\> get-process powershell | select *

I'll let you try it out for yourself.

There are a few other ways to use Select-Object and I encourage you to take a look at the cmdlet's help.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular