Select-Object

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. Wildcards are permitted.
       This parameter is only effective when the -Property parameter is also included.
 
   -ExpandProperty string
       A property to select and (attempt to) expand.
       Wildcards are permitted in the property name.

       Use -ExpandProperty to prevent the output from being truncated when exporting
       to a plain text file.

       If the specified property is an array, each value of the array should be
       included in the output.
       If the property contains an object, the full properties of that object (typically
       an array of string values) will be displayed in the output.

   -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.

       When the -InputObject parameter is used to submit a collection of items, Select-Object receives
       one object that represents the collection and returns the collection unchanged.

       To select multiple individual items, pipe them to Select-Object.

       This parameter is an implementation detail: its purpose is to enable input via the pipeline, and its
       direct use with arrays (collections) does not (yet) provide any useful functionality.

   -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.

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. These can also be entered using the shorthand "E" for "Expression" and "N" for "Name"

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 a list of file sizes in kilobytes using a calculated property:

PS C:\> Get-ChildItem C:\Demo | Select-Object Name, CreationTime, @{Name="Kbytes";Expression={$_.Length / 1Kb}}

The same using shorthand:

PS C:\> Get-ChildItem C:\Demo | Select-Object Name, CreationTime, @{N="Kbytes";E={$_.Length / 1Kb}}

Display the sum of all files in the current directory and return a single number:

PS C:\> get-childitem | measure-object -property length -sum | Select-Object -expand sum

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

“The most important motivation for the research work that resulted in the relational model was the objective of providing a sharp and clear boundary between the logical and physical aspects of database management” ~ E. F. Codd

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.


 
Copyright © 1999-2024 SS64.com
Some rights reserved