Select-Object: Filtering Output in PowerShell

PowerADM.com / PowerShell / Select-Object: Filtering Output in PowerShell

The Select-Object cmdlet allows you to display certain properties of an object in a PowerShell command. Select-Object can select unique objects, a specified number of objects, or objects at a specified position in an array. Also, some PowerShell cmdlets don’t display all of the object’s properties, and you must use Select-Object to display additional attributes.

In order to display all the properties of objects in PowerShell, you need to pipe the object to the Select-Object cmdlet (or use the Select alias). Compare how the results of the following two commands differ:

Get-Service -Name wuauserv
Get-Service -Name wuauserv | Select-Object -Property *

Get all PowerShell object properties using Select-Object cmdlet

The * character indicates that you want to list all the object properties. You can display the values of only certain attributes of an object; just specify their list separated by commas:

Get-Service -Name wuauserv | Select-Object Name,StartupType,BinaryPathName

You can also list all attributes except those specified:

Get-Service -Name wuauserv | Select-Object * -ExcludeProperty Description, DependentServices, DelayedAutoStart

You can use Select-Object to add additional columns to the output with new values. For example, you want to get information about free disk space and immediately add an additional column with the percentage of free space:

Get-Volume | select -Property DriveLetter, @{Label='Free'; Expression = {[string]([int]($_.SizeRemaining/$_.Size*100)) + " %"}}

In this example, we have added a calculated property using Select-Object. The values in the Free column were obtained by performing a mathematical operation on other attribute values.

Add calculated property using Select-Object

If an object stores multiple values, you can display all of them using the ExpandProperty parameter.

$myobject = [pscustomobject]@{Type='array'; collection=@(1,2,3,4,5)}

By default, the value of the collection attribute is output as an array of objects. To get their values, you need to add the –ExpandProperty option:

$myobject | Select-Object -Property Type -ExpandProperty collection

Using ExpandProperty for Select-Object

With the First and Last parameters, you can get the first or last element of an array. For example, the following command will list the single process that uses the most RAM:

Get-Process | Sort-Object WS -Descending | Select-Object -First 1

select-object first and last

To list the top 10 processes by CPU usage:

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

You can also access the elements of an object using the –Index parameter (index values start at 0).

Get-Process | Sort-Object WS -Descending | Select-Object -Index 0

If you need to get the first and last objects, you can use this command:

$logs= Get-WinEvent -LogName "Windows PowerShell"
$logs| Select-Object -Index 0, ($logs.count - 1)

With the -Unique option, you can display only objects with unique values:

Get-Process| select name -Unique

Note. The Unique parameter is processing case-sensitive object values.

Leave a Reply

Your email address will not be published. Required fields are marked *