Quick PowerShell tip: how to take only a single property from a result set?
Posted: (EET/GMT+2)
Today's topic is a quick tip about PowerShell. Assume for instance you need to list certain items, and these items contain numerous properties of which you need only one.
For instance, you might be enumerating the currently running processes with the "Get-Process" cmdlet. This command would return a process object with around half a dozen properties:
[PS]> Get-Process
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
119 4 12776 14572 48 1516 audiodg
44 2 1460 3420 34 0,05 3160 conime
533 5 1568 5008 108 500 csrss
541 8 17304 18792 190 564 csrss
...
But what if you wanted to list only for instance the process names currently running? How would you filter that? Enter the "select" cmdlet, which is an alias for Select-Object. After this, you could write something like this:
[PS]> Get-Process | Select-Object ProcessName ProcessName ----------- audiodg conime csrss csrss ...
In addition to simply specifying the column (property) you prefer, you can also select top N or last N objects (records), select multiple properties (separate them with a comma), and more. To learn about this, check the TechNet documentation page.
Keywords: filter results, select single property, single column only.