Using PowerShell Select-Object

PowerShell

PowerShell is an object-oriented shell for Windows, Linux, and macOS. And as such, you will need to understand how to work with PowerShell objects to get the best out of it. In this article, PowerShell expert Jeff Hicks takes you through the different ways you can use the PowerShell Select-Object cmdlet.

I think by nature most people are picky. In PowerShell, this is actually a positive attribute. PowerShell is designed to give you a lot of information. Sometimes more than you really need. PowerShell is also designed to display command results that someone from Microsoft, or a vendor in the world of third-party cmdlets, thought you would most likely want to see or use.

Of course, you know that there is always much more to the picture than what you see. Once you discover what you can use, you can afford to be a bit pickier. And the cmdlet that makes this easy is Select-Object. If you are new to using PowerShell objects, read Introduction to Objects in PowerShell on Petri.

2 ways to use the PowerShell Select-Object cmdlet

There are a few ways to use Select-Object. Perhaps the most important way is to use it to display a subset of object properties.

1. Display a subset of object properties

You should know that properties you see when you run a command like Get-Process are not the only properties. In some cases, what you see isn’t even the actual property name. The best way is to use Get-Member to discover the actual property names.

get-process | get-member -MemberType Properties
Viewing object properties with Get-Member
Viewing object properties with Get-Member (Image Credit: Jeff Hicks)

Once you know the property names, you can run the command and pipe it to Select-Object, specifying the property names you want to see.

get-process | select-object -Property ID,Name,WS,Path
Selecting specific process properties with PowerShell Select-Object
Selecting specific process properties (Image Credit: Jeff Hicks)

When you use Select-Object, you get the raw property value. In some cases, such as with Get-Process, PowerShell has special rules to format the output. You may need to do something similar.

For example, the WorkingSet property (using the WS alias) is being displayed in bytes. Perhaps I need to see that value in KB. Using a specific hashtable structure, I can define a custom property.

The hashtable has two keys, Name and Expression. The value for Name is whatever you want to call your property. The value for Expression is a scriptblock. PowerShell will evaluate the scriptblock and use the result as the output. You can use as much PowerShell code as you need. If you want to reference the current object, use the $_ construct.

Note that in the code below, Select is used as an alias for Select-Object.

get-process | Select ID,Name,@{Name="WS(KB)"; Expression = { $_.WS/1kb}},Path

The hashtable will define a property called WS(KB), which will have a value of the current WS value divided by 1KB.

Using a custom property
Using a custom property (Image Credit: Jeff Hicks)

I love using this hashtable trick because you can create entirely new properties to fit your needs.

dir c:\work -file | Select Name,@{Name="Size";Expression={$_.length}},CreationTime,LastWriteTime,@{Name="ModifiedAge";Expression={(Get-Date) - $_.lastwritetime}} | sort ModifiedAge -Descending | Out-Gridview -Title "Work Files"

I created a few custom properties here. The first is essentially an alias property for file length that I’m calling Size, which I find more meaningful. I also created a new property, called ModifiedAge, that calculates a timespan indicating how long it has been since the file was modified.

When you create these types of custom properties with Select-Object, they are passed on in the pipeline, which means you use can use them with other commands such as Sort-Object. That is exactly what I am doing here. I’m sorting on the new ModifiedAge property and piping the results to Out-Gridview.

Using a custom property in the pipeline
Using a custom property in the pipeline (Image Credit: Jeff Hicks)

This brings me to the other way you are likely to use Select-Object.

2. Select the last X number of objects


Sometimes you only want the first or last X number of objects. For instance, using my previous example, perhaps I only want to see the 10 oldest files. Because they are sorted in descending order, I can use the -First parameter with Select-Object.

CreationTime,LastWriteTime,@{Name="ModifiedAge";Expression={(Get-Date) - $_.lastwritetime}} | sort ModifiedAge -Descending | Select -first 10 | format-table

There’s nothing wrong with using Select-Object twice. In this case, I have to because I need to define my custom property and then sort on it before I can select the first 10 objects.

Selecting first 10 objects with PowerShell Select-Object
Selecting first 10 objects (Image Credit: Jeff Hicks)

Although you can certainly select key properties and specify first or last in the same command.

Selecting properties and a subset of objects
Selecting properties and a subset of objects (Image Credit: Jeff Hicks)

It all depends on what you need to accomplish.

Conclusion

There are a few other parameters you might use with Select-Object, so be sure to take a few minutes to read complete help and examples. But I think the majority of your work with Select-Object will be using the techniques I’ve demonstrated here.

I think Select-Object is one of the cmdlets that you will use constantly, so take some time to learn how to use it to your advantage.

If you want to learn about filtering with PowerShell, read Filtering PowerShell on Petri.