Select-Object

Applies To: Windows PowerShell 2.0

Selects specified properties of an object or set of objects. It can also select unique objects from an array of objects, or it can select a specified number of objects from the beginning or end of an array of objects.

Syntax

Select-Object [[-Property] <Object[]>] [-ExcludeProperty <string[]>] [-ExpandProperty <string>] [-First <int>] [-InputObject <psobject>] [-Last <int>] [-Skip <int>] [-Unique] [<CommonParameters>]

Select-Object [-Index <Int32[]>] [-InputObject <psobject>] [-Unique] [<CommonParameters>]

Description

The Select-Object cmdlet gets only the specified properties of an object or set of objects. It can also select unique objects from an array of objects, or it can select a specified number of objects from the beginning or end of an array of objects.

If you use Select-Object to select specified properties, it copies the values of those properties from the input objects and creates new objects that have the specified properties and copied values.

Use the Property parameter to specify the properties you want to select. Or, use the First, Last, Unique, Skip, and Index parameters to select particular objects from an array of input objects. For more specific object filtering, use the Where-Object cmdlet.

Parameters

-ExcludeProperty <string[]>

Removes the specifies properties from the selection. Wildcards are permitted. This parameter is effective only when the command also includes the Property parameter.

The value of the property parameter can be a calculated property, which is a hash table that specifies a name and calculates a value for the property display. Valid keys are:

-- Name or Label <string>

-- Expression <string> or <scriptblock>

For more information, see the examples.

Required?

false

Position?

named

Default Value

Accept Pipeline Input?

false

Accept Wildcard Characters?

true

-ExpandProperty <string>

Specifies a property to select, and indicates that an attempt should be made to expand that property. Wildcards are permitted in the property name.

For example, if the specified property is an array, each value of the array is included in the output. If the property contains an object, the properties of that object are displayed in the output.

Required?

false

Position?

named

Default Value

Accept Pipeline Input?

false

Accept Wildcard Characters?

true

-First <int>

Specifies the number of objects to select from the beginning of an array of input objects.

Required?

false

Position?

named

Default Value

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Index <Int32[]>

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

Required?

false

Position?

named

Default Value

None

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-InputObject <psobject>

Specifies objects to send to the cmdlet through the pipeline. This parameter enables you to pipe objects to Select-Object.

Required?

false

Position?

named

Default Value

Accept Pipeline Input?

true (ByValue)

Accept Wildcard Characters?

false

-Last <int>

Specifies the number of objects to select from the end of an array of input objects.

Required?

false

Position?

named

Default Value

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Property <Object[]>

Specifies the properties to select. Wildcards are permitted.

The value of the Property parameter can be a new calculated property. To create a calculated, property, use a hash table. Valid keys are:

-- Name (or Label) <string>

-- Expression <string> or <script block>

Required?

false

Position?

1

Default Value

Accept Pipeline Input?

false

Accept Wildcard Characters?

true

-Skip <int>

Skips (does 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.

Required?

false

Position?

named

Default Value

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Unique

Selects only unique objects. If a collection of selected objects includes objects that are identical, this parameter selects only unique objects from the collection.

This parameter is case-sensitive. As a result, strings that differ only in character casing are considered to be unique.

Required?

false

Position?

named

Default Value

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

<CommonParameters>

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -OutBuffer, -OutVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

Inputs and Outputs

The input type is the type of the objects that you can pipe to the cmdlet. The return type is the type of the objects that the cmdlet returns.

Inputs

System.Management.Automation.PSObject

You can pipe any object to Select-Object.

Outputs

System.Management.Automation.PSObject

Notes

You can also refer to Select-Object by its built-in alias, "select". For more information, see about_Aliases.

Example 1

C:\PS>get-process | select-object ProcessName,Id,WS

Description
-----------
This command displays a list of processes. Only the name, ID, and working set (WS) properties of the processes are displayed.





Example 2

C:\PS>get-process | select-object processname -expandproperty modules |
format-list

ProcessName       : 00THotkey
Size              : 256
Company           : TOSHIBA Corporation
FileVersion       : 1, 0, 0, 27
ProductVersion    : 6, 2, 0, 0
Description       : THotkey
Product           : TOSHIBA THotkey
ModuleName        : 00THotkey.exe
FileName          : C:\WINDOWS\system32\00THotkey.exe
BaseAddress       : 4194304

Description
-----------
This command displays information about the modules used by the processes running on a computer. It uses the ExpandProperty parameter to display the details contained within the modules property.





Example 3

C:\PS>get-process | sort-object -property WS | select-object -Last 5

Handles  NPM(K)    PM(K)      WS(K) VS(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
   2866     320    33432      45764   203   222.41   1292 svchost
    577      17    23676      50516   265    50.58   4388 WINWORD
    826      11    75448      76712   188    19.77   3780 Ps
   1367      14    73152      88736   216    61.69    676 Ps
   1612      44    66080      92780   380   900.59   6132 INFOPATH

Description
-----------
This command displays the five processes that are using the most memory. The Sort-Object cmdlet is used to sort the processes according to memory (working set) usage, and the Select-Object cmdlet is used to select only the last five members of the resulting array of objects.





Example 4

C:\PS>get-process | select-object -property ProcessName,@{Name="Start Day"; Expression = {$_.StartTime.DayOfWeek}}

ProcessName  StartDay
----         --------
alg          Wednesday
ati2evxx     Wednesday
ati2evxx     Thursday
...

Description
-----------
This command displays the name and start day of the processes running on a computer. 

The values of the Property parameter are ProcessName and a calculated property named "Start Day." The "Start Day" property is added by using a hash table with Name and Expression keys.





Example 5

C:\PS>"a","b","c","a","a","a" | select-object -unique

a
b
c

Description
-----------
This command displays unique characters from an array of characters.





Example 6

C:\PS>$a = get-eventlog -log "Windows PowerShell" 

C:\PS> $a | select-object -index 0, ($a.count - 1)

Description
-----------
These commands get the first (newest) and last (oldest) events in the Windows Powershell event log. 

The first command uses the Get-EventLog cmdlet to get all events in the Windows Powershell log. It saves the events in the $a variable.

The second command uses a pipeline operator (|) to send the events in $a to the Select-Object cmdlet. The Select-Object command uses the Index parameter to select items by their index number. The index for the first event is 0. The index for the last event is the number of items in $a minus 1.





Example 7

C:\PS>new-pssession -computername (get-content servers.txt | select-object -skip 1)

Description
-----------
This command creates a new PSSession on each of the computers listed in the Servers.txt files, except for the first one.

This command uses the Select-Object cmdlet to select all but the first computer in a list of computer names. The resulting list of computers is set as the value of the ComputerName parameter of the New-PSSession cmdlet.





See Also

Concepts

Where-Object
Group-Object
Sort-Object