One of PowerShell’s biggest strengths is how it handles objects. Unlike traditional command-line tools, PowerShell doesn’t just deal with text—it works with objects that have properties you can query, filter, and manipulate.

The Select-Object cmdlet is a versatile tool that allows you to control and filter the output of your commands by selecting specific properties, creating custom columns, or limiting the number of results. This is extremely helpful when you want to focus on specific details or clean up the output of a command.

Let’s walk through how to use Select-Object effectively.

Basic Syntax

Here’s the basic syntax of Select-Object:

1
<Command> | Select-Object -Property <Property1>, <Property2>, ...

is any PowerShell command or pipeline.
-Property specifies which object properties to include in the output.

Selecting Specific Properties

By default, PowerShell outputs all the properties of an object, but often you only care about a subset of that data. Select-Object helps you zero in on what’s important.

For example, let’s get a list of running processes:

Get-Process

By default, this outputs many properties, including the process name, ID, CPU usage, and more.

To select just the Name and Id properties:

Get-Process | Select-Object -Property Name, Id

1
2
3
4
5
Name                     Id
----                     --
powershell              1234
explorer                5678
svchost                 4321

Creating Custom Properties

You can also use Select-Object to add custom properties to your output using calculated expressions. This is useful when you want to manipulate or display data in a specific way.

The syntax for a calculated property is:

@{Name = ‘CustomName’; Expression = { YourExpressionHere }}

Let’s add a custom column that calculates CPU usage in seconds:

1
2
Get-Process | Select-Object -Property Name, Id, `
    @{Name = 'CPUSeconds'; Expression = { [math]::Round($_.CPU, 2) }}

Here’s what’s happening:
• Name = ‘CPUSeconds’ specifies the custom column’s name.
• Expression runs a script block ({ … }) to calculate the value.
$_ represents the current object in the pipeline (in this case, a process object). <br /

Output:

1
2
3
4
5
Name                     Id CPUSeconds
----                     -- ----------
powershell              1234       1.24
explorer                5678      10.56
svchost                 4321       2.11

Limiting the Number of Results

Sometimes you only need a subset of results. Use -First or -Last with Select-Object to limit how many objects you retrieve.

  1. Get the First N Results:

Get-Process | Select-Object -First 5

This outputs the first 5 processes.

  1. Get the Last N Results:

Get-Process | Select-Object -Last 5

This retrieves the last 5 processes.

Skipping Objects with -Skip

You can also use -Skip to exclude a specified number of results at the beginning of the output.

For example, skip the first 10 results:

Get-Process | Select-Object -Skip 10

This is especially helpful when you’re paging through large sets of data.

Selecting Unique Results

If your data contains duplicates, you can use the -Unique parameter to filter them out.

For example, let’s list the unique names of running processes:

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

Real-World Example: Filtering Files

Let’s combine Get-ChildItem (to list files) and Select-Object to get a report of the 5 largest files in a folder.

1
2
3
Get-ChildItem -Path "C:\SomeFolder" -Recurse | `
    Sort-Object -Property Length -Descending | `
    Select-Object -Property Name, Length -First 5

Here’s what’s happening:

  1. Get-ChildItem -Recurse retrieves all files in the folder and subfolders.
  2. Sort-Object -Property Length -Descending sorts the files by size, largest first.
  3. Select-Object -First 5 limits the output to the top 5 files.

Output:

1
2
3
4
5
Name                           Length
----                           ------
BigFile1.zip                 10485760
VideoFile.mp4                 5242880
LargeDoc.pdf                  1048576

Best Practices for Using Select-Object

  1. Filter Early: Use Select-Object early in the pipeline to reduce the amount of data passed through subsequent commands.
  2. Use Custom Properties Wisely: Calculated expressions can be powerful but may impact performance if overused.
  3. Combine with Sorting: Use Sort-Object before Select-Object to control which results are included.

Summary

The Select-Object cmdlet is a must-know tool for anyone working with PowerShell. It allows you to:
• Filter specific properties from objects.
• Create custom properties with calculated expressions.
• Limit the number of results with -First, -Last, or -Skip.
• Remove duplicates with -Unique.

By mastering Select-Object, you can streamline your scripts, format output to meet your needs, and focus on what’s truly important in your data.

Happy scripting! 🚀