Select Object - PowerShell
Overview
Select-Object extracts specific properties from objects in PowerShell, allowing you to filter and display only the desired data.
Syntax
Select-Object <Property1>, <Property2>, ... [<AdditionalParameters>]
Parameters
| Parameter | Description |
|—|—|
| Property1, Property2, ...
| Properties to be selected. |
| -First <number>
| Selects the first
| -Last <number>
| Selects the last
| -Skip <number>
| Skips the first
| -ExpandProperty <PropertyName>
| Expands nested properties. |
| -Unique
| Selects only unique objects. |
Options/Flags
- -ExcludeProperty: Excludes specified properties.
- -PassThru: Passes all objects through the pipeline with selected properties.
- -ErrorAction: Specifies the action to be taken when an error occurs (default:
Stop
). - -WarningAction: Specifies the action to be taken when a warning occurs (default:
Inquire
).
Examples
Simple Selection
PS> Get-Process | Select-Object Name, Id
Complex Selection
PS> Get-Process | Select-Object -First 5 -Skip 2 -ExpandProperty PrivateMemory
Combining Options
PS> Get-EventLog -LogName System | Select-Object EventId, Source -ExcludeProperty TimeWritten -PassThru
Common Issues
- Missing Property: If a specified property does not exist, an error will be thrown. Use
-ErrorAction Ignore
to suppress errors. - Data Truncation: If selected properties are too long, they may be truncated in the output. Use
-ExpandProperty
or increase terminal window width.
Integration
Select-Object can be chained with other commands for advanced filtering and processing:
Get-ChildItem | Select-Object -Unique | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-30) }