PowerShell Select-Object [With Examples]

As a PowerShell developer, you should know how to use the PowerShell Select-Object cmdlet. In this tutorial, I will explain the cmdlet’s syntax and provide a few examples.

The Select-Object cmdlet in PowerShell allows you to filter and select specific properties from objects. For example, if you want to display only the process name and ID from a list of running processes, you can use the command Get-Process | Select-Object -Property Name, Id. This will output the list containing just the Name and Id properties, helping you focus on the essential information.

What is Select-Object in PowerShell?

The Select-Object PowerShell cmdlet allows you to select specific properties from objects or create new properties. This is particularly useful when you need to filter out unnecessary data and focus on the information that matters most.

Syntax of PowerShell Select-Object

Here is the basic syntax for Select-Object:

Select-Object [-Property] <Object[]> [-ExcludeProperty <String[]>] [-ExpandProperty <String>] [-Unique] [-InputObject <PSObject>] [-First <Int32>] [-Last <Int32>] [<CommonParameters>]

Below are some of the key parameters:

  • -Property: Specifies the properties to select.
  • -ExcludeProperty: Excludes specified properties from the output.
  • -ExpandProperty: Expands a property that is an object or array.
  • -Unique: Ensures that only unique objects are selected.
  • -First: Selects the first specified number of objects.
  • -Last: Selects the last specified number of objects.

Also, check out PowerShell Copy-Item

PowerShell Select-Object Examples

Now, let me show you a few examples of using the Select-Object PowerShell cmdlet. You will learn how to use this cmdlet.

1. Select Specific Properties

Suppose you have a list of services running on your system, and you want to display only the service name and status. You can achieve this with Select-Object. Below is the PowerShell script.

Get-Service | Select-Object -Property Name, Status

This command will output a list of services with just the Name and Status properties.

You can see the exact output in the screenshot below:

PowerShell Select-Object

2. Rename Properties

You might want to rename properties for better readability. For example, let’s say you want to rename Name to ServiceName. You can do this by using the below script:

Get-Service | Select-Object -Property @{Name='ServiceName';Expression={$_.Name}}, Status

Here, we use a hash table to specify the new name and the expression to extract the original property.

Check out PowerShell Write-Output

3. Select Unique Objects

If you have a collection of network adapters and you want to display only unique adapter names, you can use the -Unique parameter of Select-Object PowerShell cmdlet. Consider a scenario where you have a list of network adapters:

Get-NetAdapter | Select-Object -Property Name -Unique

This command will ensure that only unique adapter names are displayed.

4. Select the First or Last N Objects

Sometimes, you might be interested in only the first or last few objects from a collection. You can use the Select-Object cmdlet to get the first or last N objects.

For instance, to get the first 3 services:

Get-Service | Select-Object -First 3

Here is the exact output in the screenshot below:

PowerShell Select-Object Examples

Or to get the last 3 services:

Get-Service | Select-Object -Last 3

Check out PowerShell Write-Host vs Out-Host

5. Expand Properties

When dealing with nested objects, you might need to expand a property to access its contents. For example, if you have a property that contains an array of IP configurations:

Get-NetIPAddress | Select-Object -ExpandProperty IPAddress

This command will expand the IPAddress property, displaying the details of each IP address configuration.

6. Create Custom Objects

You can also use the PowerShell Select-Object to create custom objects with calculated properties. For example, let’s create a custom object that includes the service name, status, and a calculated property for start type:

Get-Service | Select-Object -Property Name, Status, @{Name='StartType';Expression={$_.StartType}}

This command adds a new property StartType to the output.

Here is the exact output in the screenshot below:

Select-Object in PowerShell

Read Create Custom Objects in PowerShell

7. Filter Data Based on Conditions

You can combine Select-Object with other cmdlets, like Where-Object to filter data based on specific conditions in PowerShell. For example, to list services that are currently running; here is the script.

Get-Service | Where-Object {$_.Status -eq 'Running'} | Select-Object -Property Name, Status

This command filters services based on their status and then selects the relevant properties.

I executed the above PowerShell script using VS code, and you can see the exact output in the screenshot below:

Select-Object in PowerShell examples

Check out PowerShell Select-Object Without Header

PowerShell select-object order by

The Select-Object cmdlet in PowerShell is often used in combination with the Sort-Object cmdlet to order the results based on specific properties. Here’s a practical example to understand how you can use Select-Object and Sort-Object together.

Example: Order Services by Status

Suppose you want to list services on your system and order them by their status (Running, Stopped, etc.) and then select only the service name and status properties.

Here’s how you can achieve this:

Get-Service | Sort-Object -Property Status | Select-Object -Property Name, Status

Explanation:

  1. Get-Service: This cmdlet retrieves all the services on the system.
  2. Sort-Object -Property Status: This sorts the services by their Status property. By default, sorting is done in ascending order.
  3. Select-Object -Property Name, Status: This selects only the Name and Status properties from the sorted list of services.

Output:

The command will output a list of services ordered by their status, showing only the Name and Status properties. For example, in the output below screenshot, you can see that the services are ordered by their status, with all stopped services listed first, followed by running services.

PowerShell select-object order by

Check out PowerShell Select-Object -Unique

PowerShell select-object expression Examples

One of the powerful features of Select-Object is the ability to use expressions to create calculated properties. This is particularly useful when you need to manipulate data on the fly.

Suppose you have a list of employees, and you want to create a custom property that combines their first and last names into a full name. Here’s how you can achieve that:

$employees = @(
    [PSCustomObject]@{FirstName="John"; LastName="Doe"; JobTitle="Manager"},
    [PSCustomObject]@{FirstName="Jane"; LastName="Smith"; JobTitle="Developer"},
    [PSCustomObject]@{FirstName="Emily"; LastName="Johnson"; JobTitle="Designer"}
)

$selectedEmployees = $employees | Select-Object FirstName, LastName, JobTitle, @{Name="FullName"; Expression={$_.FirstName + " " + $_.LastName}}
$selectedEmployees

Explanation

  • Input Data: A list of employees with properties FirstNameLastName, and JobTitle.
  • Select-Object: We use Select-Object to select the existing properties and create a new property called FullName.
  • Expression: The Expression key in the hash table concatenates FirstName and LastName.

When you execute the PowerShell script, you can see the exact output in the screenshot below.

powershell select-object expression examples

Also, check out PowerShell Select-Object -First

PowerShell Select-Object Filter

Now, let me explain how to use the filter in PowerShell Select-Object with Where-Object and provide a few useful examples.

You can combine Select-Object and Where-Object to create various filters. For example, let’s find all services that are running and select their DisplayName and Status:

Get-Service | Where-Object { $_.Status -eq 'Running' } | Select-Object DisplayName, Status

This command first filters the services that are running and then selects only the DisplayName and Status properties.

Here is the exact output you can see in the screenshot below:

powershell select-object filter

Conclusion

The Select-Object cmdlet in PowerShell allows you to manipulate and filter data efficiently. In this tutorial, I explained how to use the PowerShell Select-Object cmdlet with a few examples like renaming properties, selecting unique objects, or creating custom calculated properties, etc.

I hope this guide has given you a complete understanding of how to use Select-Object in PowerShell.

Note: For execution of all the above PowerShell scripts, I used Visual Studio Code; you can use any other preferred editors.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.