0
0
PowershellHow-ToBeginner · 3 min read

How to Use Select-Object in PowerShell: Syntax and Examples

Use the Select-Object cmdlet in PowerShell to pick specific properties or a set number of objects from a collection. It lets you filter output by selecting only the properties you want or limiting the number of results.
📐

Syntax

The basic syntax of Select-Object includes specifying which properties to select from input objects. You can also limit the number of objects returned or create new calculated properties.

  • -Property: Choose which properties to keep.
  • -First: Select only the first N objects.
  • -Last: Select only the last N objects.
  • -Unique: Return only unique objects.
  • -ExpandProperty: Extract the value of a single property.
powershell
Get-Process | Select-Object -Property Id, ProcessName -First 5
💻

Example

This example shows how to get the first 3 running processes and select only their Id and ProcessName properties.

powershell
Get-Process | Select-Object -Property Id, ProcessName -First 3
Output
Id ProcessName -- ----------- 1234 powershell 5678 explorer 9101 notepad
⚠️

Common Pitfalls

One common mistake is trying to select properties that do not exist on the objects, which results in empty or missing columns. Another is forgetting that Select-Object works on objects passed through the pipeline, so using it without input produces no output.

Also, using -ExpandProperty incorrectly can cause errors if the property does not exist or contains multiple values.

powershell
Get-Process | Select-Object -Property Id, FakeProperty

# Correct usage:
Get-Process | Select-Object -Property Id, ProcessName
📊

Quick Reference

ParameterDescription
-PropertySelect specific properties from objects
-FirstReturn only the first N objects
-LastReturn only the last N objects
-UniqueReturn only unique objects
-ExpandPropertyExtract the value of a single property

Key Takeaways

Use Select-Object to pick specific properties or limit output objects in PowerShell.
Always verify the properties you select exist on the objects to avoid empty results.
Use -First or -Last to control how many objects you get from the pipeline.
-ExpandProperty extracts a single property's value instead of an object.
Select-Object works on objects passed through the pipeline, so ensure input is provided.