PowerShell Select-Object Tutorial

The Select-Object is used to select and use specified properties of an object which is generally an output of a command. Besides selecting different properties it can be also used to select unique objects from multiple objects or one item from an array. The Select-Object command returns another object which is the selection. Also, the Select-Object can be used to optimize objects to prevent processing for unused objects.

Select Object By Property

The basic features of the Select-Object are selecting the specified attributes for the specified object. The -Property attribute is used to provide the attribute names and multiple attributes can be separated with commas. Below we will use the Get-Process command to list all currently running processes as an object and redirect this object to the Select-Object and then select ProcessName, Id, and CPU attributes.

 Get-Process | Select-Object -Property ProcessName, Id, CPU

The output will be like below. We can see that all provided attributes ProcessName, Id, and CPU printed for every processes currently running.

Select Object By Property

Select Object and Extend Property

The -ExpandProperty attribute can be used to print detailed and verbose information about the specified attribute. In the following example, we will print detailed information about the Modules property of the ProcessName attributes. Also, the Format-List can be used to print output in a more readable and formatted way.

Get-Process Chrome| Select-Object -Property ProcessName -ExpandProperty Modules | Format-List

Select First or Last Range For Specified Attribute

The Select-Object command provides the -First and -Last attributes in order to select first and last items for the given list of objects. Keep in mind there is no Attribute that will be selected we will just select the first or list item of the specified list of objects.

Get-Process | Sort-Object -Property ID | Select-Object -First 10
Select First Items with Select-Object

Also, we can use the -Last attribute in order to select from the last of the given list for the specified number of objects. In the following example, we will list the process according to the process ID and select the last processes which mean the biggest process IDs.

Get-Process | Sort-Object -Property ID | Select-Object -First 10
Select Last Items with Select-Object

Select Most Memory Using Process

One of the most popular use cases for the Select-Object command is selecting the most memory using processes. We will also use the Sort-Object command in order to sort according to the memory usage which is expressed as -Property WS.

Get-Process | Sort-Object -Property WS | Select-Object -Last 10
Select Most Memory Using Process

Select Most CPU Using Process

Another popular use case for the Select-Object is listing the most CPU using processes. Again we will use the Sort-Object for sort operation and Select-Object in order to select the top CPU using processing.

Get-Process | Sort-Object -Property CPU | Select-Object -Last 10
Select Most CPU Using Process

Select Unique Values From Array

The Select-Object provides the -Unique attribute which can be used to select only unique elements like a set. The same element will not be selected multiple times.

1,5,3,2,5,7,8,0,4,3 | Select-Object -Unique

Select Latest (Newest) and First (Oldest) Event From Event Logs

By using the Get-EventLog command latest (newest) or first (oldest) event logs can be searched and listed easily. In the following example, we will first store the System logs into the variable named $logs and then select the lastest logs.

$logs = Get-EventLog  -LogName "System"

$logs | Select-Object -First 10

Also, the first or older logs can be listed with the following commands which are very same as the previous example.

$logs = Get-EventLog  -LogName "System"

$logs | Select-Object -First 10
Select Latest (Newest) and First (Oldest) Event From Event Logs

Leave a Comment