Select properties of an object or set of objects. Select objects from an array.
Syntax Select-Object [[-property] Object[]] [-excludeProperty string[]] [-expandProperty string] [-first int] [-last int] [-Skip int] [-unique] [-inputObject psobject] [CommonParameters] Select-Object [-Index Int32[]] [-InputObject psobject] [-Unique] [CommonParameters] Key -Property Object[] The property or properties to select. -ExcludeProperty string Properties that will not be selected. -ExpandProperty string Select and expand the specified property. If the specified property is an array, for example, each value of the array should be included. -First int Select int number of objects from the beginning of an array of input objects. -Last int Select int number of objects from the end of an array of input objects. -Skip int Skip (do not select) the specified number of items. By default, the Skip parameter counts from the beginning of the array or list of objects, but if the command uses the Last parameter, it counts from the end of the list or array. Unlike the Index parameter, which starts counting from 0, the Skip parameter begins at 1. -Unique Select unique objects only, (identical properties and values) -InputObject psobject An object or objects to input to Select-Object. May be pipelined. -Index Int32[] Select objects from an array based on their index values. Enter the indexes in a comma-separated list. Indexes in an array begin with 0, where 0 represents the first value and (n-1) represents the last value. CommonParameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable, -OutBuffer -OutVariable.
Standard Aliases for Select-Object: select
Select-Object will create new objects by copying the values of the selected properties from the input objects.
If the input object is an array, the -First, -Last and -Unique parameters may be used to select particular objects, for more powerful object filtering, use Where-Object.
To add a calculated property to an object, specify a hash table as a value of the -Property parameter. The hash table must include two keys: Name and Expression with the Expression key assigned a script block that will determine the value of the property.
Examples
Display only the name, ID and Working Set(WS) properties of Get-Process:
PS C:\> get-process | select-object ProcessName,Id,WS
Display only the Name and modules properties of Get-Process, use -ExpandProperty to display the details contained within the modules property:
PS C:\> get-process | select-object ProcessName -expandproperty modules | format-list
Display the 5 processes that are using the most memory (WS=Working Set):
PS C:\> get-process | sort-object -property WS | select-object -Last 5
Display the name and claculate the start day of the processes running:
PS C:\> get-process | select-object ProcessName,@{Name="Start Day"; Expression={$_.StartTime.DayOfWeek}}
Get the first (newest) and last (oldest) events in the Windows PowerShell event log:
PS C:\> $evts = get-eventlog -log "Windows PowerShell"
PS C:\> $evts | select-object -index 0, ($evts.count - 1)
Retrieve all the names listed in the Servers.txt file, except for the first one:
PS C:\> get-content servers.txt | select-object -skip 1
“A select committee expires on completion of its assigned duties” ~ Wikipedia
Related PowerShell Cmdlets:
Select-String - Search through strings or files for patterns.
Compare-Object Compare the properties of objects.
ForEach-Object - Loop for each object in the pipeline.
Group-Object - Group the objects that contain the same value for a common property.
Measure-Object - Measure aspects of object properties and create objects from those values.
New-Object - Create a new .Net object.
Sort-Object - Sort the input objects by property value.
Tee-Object - Send input objects to two places.
Where-Object - Filter input from the pipeline allowing operation on only certain objects.
Get-Unique - Get the unique items in a collection.
Equivalent bash command: gawk - Find and Replace text.