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:
|
|
•
• -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
|
|
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:
|
|
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:
|
|
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.
- Get the First N Results:
Get-Process | Select-Object -First 5
This outputs the first 5 processes.
- 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.
|
|
Here’s what’s happening:
- Get-ChildItem -Recurse retrieves all files in the folder and subfolders.
- Sort-Object -Property Length -Descending sorts the files by size, largest first.
- Select-Object -First 5 limits the output to the top 5 files.
Output:
|
|
Best Practices for Using Select-Object
- Filter Early: Use Select-Object early in the pipeline to reduce the amount of data passed through subsequent commands.
- Use Custom Properties Wisely: Calculated expressions can be powerful but may impact performance if overused.
- 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! 🚀