Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

Select-Object

Updated: October 17, 2013

Applies To: Windows PowerShell 4.0

Select-Object

Selects objects or object properties.

Aliases

The following abbreviations are aliases for this cmdlet:

  • select

Syntax

Parameter Set: DefaultParameter
Select-Object [[-Property] <Object[]> ] [-ExcludeProperty <String[]> ] [-ExpandProperty <String> ] [-First <Int32> ] [-InputObject <PSObject> ] [-Last <Int32> ] [-Skip <Int32> ] [-Unique] [-Wait] [ <CommonParameters>]

Parameter Set: IndexParameter
Select-Object [-Index <Int32[]> ] [-InputObject <PSObject> ] [-Unique] [-Wait] [ <CommonParameters>]




Detailed Description

The Select-Object cmdlet selects specified properties of an object or set of objects. It can also select unique objects, a specified number of objects, or objects in a specified position in an array.

To select objects from a collection, use the First, Last, Unique, Skip, and Index parameters. To select object properties, use the Property parameter. When you select properties, Select-Object returns new objects that have only the specified properties.

Beginning in Windows PowerShell 3.0, Select-Object includes an optimization feature that prevents commands from creating and processing objects that are not used. When you include a Select-Object command with the First or Index parameters in a command pipeline, Windows PowerShell stops the command that generates the objects as soon as the selected number of objects is generated, even when the command that generates the objects appears before the Select-Object command in the pipeline. To turn off this optimizing behavior, use the Wait parameter.

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.


Aliases

none

Required?

false

Position?

named

Default Value

none

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.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

true

-First<Int32>

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


Aliases

none

Required?

false

Position?

named

Default Value

none

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.


Aliases

none

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.

When you use the InputObject parameter with Select-Object, instead of piping command results to Select-Object, the InputObject value—even if the value is a collection that is the result of a command, such as –InputObject (Get-Process)—is treated as a single object. Because InputObject cannot return individual properties from an array or collection of objects, it is recommended that if you use Select-Object to filter a collection of objects for those objects that have specific values in defined properties, you use Select-Object in the pipeline, as shown in the examples in this topic.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

true (ByValue)

Accept Wildcard Characters?

false

-Last<Int32>

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


Aliases

none

Required?

false

Position?

named

Default Value

none

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>


Aliases

none

Required?

false

Position?

1

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

true

-Skip<Int32>

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 at 0, the Skip parameter begins at 1.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Unique

Specifies that if a subset of the input objects has identical properties and values, only a single member of the subset will be selected.

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


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Wait

Turns off optimization. Windows PowerShell runs commands in the order that they appear in the command pipeline and lets them generate all objects. By default, if you include a Select-Object command with the First or Index parameters in a command pipeline, Windows PowerShell stops the command that generates the objects as soon as the selected number of objects is generated.

This parameter is introduced in Windows PowerShell 3.0.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

<CommonParameters>

This cmdlet supports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutBuffer, and -OutVariable. For more information, see  about_CommonParameters (http://go.microsoft.com/fwlink/p/?LinkID=113216).

Inputs

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

  • System.Management.Automation.PSObject

    You can pipe any object to Select-Object.


Outputs

The output type is the type of the objects that the cmdlet emits.

  • System.Management.Automation.PSObject

Notes

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

  • The optimization feature of Select-Object is available only for commands that write objects to the pipeline as they are processed. It has no effect on commands that buffer processed objects and write them as a collection. Writing objects immediately is a cmdlet design best practice. For more information, see "Write Single Records to the Pipeline" in "Strongly Encouraged Development Guidelines" at http://go.microsoft.com/fwlink/?LinkId=223385

Examples

-------------------------- EXAMPLE 1 --------------------------

This command creates objects that have the Name, ID, and working set (WS) properties of process objects.


PS C:\> Get-Process | Select-Object -Property ProcessName, Id, WS

-------------------------- EXAMPLE 2 --------------------------

This command gets information about the modules used by the processes on the computer. It uses Get-Process cmdlet to get the process on the computer. It uses the Select-Object cmdlet to create new objects with only the selected properties. The command uses the Property parameter of the Select-Object cmdlet to select the process names. Because the Modules property contains an ModuleProcess object that has many properties, the command uses the ExpandProperty parameter to get the properties of the objects in the Modules property of each process. The command uses the Format-List parameter to display the name and modules in of each process in a list.


PS C:\> Get-Process Explorer | Select-Object –Property 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

-------------------------- EXAMPLE 3 --------------------------

This command gets the five processes that are using the most memory. The Get-Process cmdlet gets the processes on the computer. The Sort-Object cmdlet sorts the processes according to memory (working set) usage, and the Select-Object cmdlet selects only the last five members of the resulting array of objects.

The Wait parameter is not required in commands that include the Sort-Object cmdlet because Sort-Object processes all objects and then returns a collection. The Select-Object optimization is available only for commands that return objects individually as they are processed.


PS C:\> 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

-------------------------- EXAMPLE 4 --------------------------

This command gets the name and start day of the processes running on a computer.

The command uses the Get-Process cmdlet to get the processes on the computer. It passes the processes to the Select-Object cmdlet, which creates objects that have only the ProcessName parameter and a calculated property named "Start Day." The "Start Day" property is added by using a hash table with Name and Expression keys. The value of the Expression key is a script blocks that gets the StartTime property of each process and the DayofWeek property of the StartTime.


PS C:\> Get-process | Select-Object -Property ProcessName,@{Name="Start Day"; Expression = {$_.StartTime.DayOfWeek}}
              
ProcessName StartDay
---- --------
alg Wednesday
ati2evxx Wednesday
ati2evxx Thursday
...

-------------------------- EXAMPLE 5 --------------------------

This command uses the Unique parameter of Select-Object to get unique characters from an array of characters.


PS C:\> "a","b","c","a","a","a" | Select-Object -Unique
              
a
b
c

-------------------------- EXAMPLE 6 --------------------------

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

The command uses the Get-EventLog cmdlet to get all events in the Windows PowerShell log. It saves them 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 events from the array of events in the $a variable. The index of the first event is 0. The index of the last event is the number of items in $a minus 1.


PS C:\> $a = Get-Eventlog -Log "Windows PowerShell"$a | select-object -index 0, ($a.count - 1)

-------------------------- EXAMPLE 7 --------------------------

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.


PS C:\> New-PSSession -ComputerName (Get-Content Servers.txt | Select-Object -Skip 1)

-------------------------- EXAMPLE 8 --------------------------

This command adds a "-ro" suffix to the base names of text files that have the read-only attribute and then displays the first five files so the user can see a sample of the effect.

The command uses the ReadOnly dynamic parameter of the Get-ChildItem for FileSystem cmdlet to get read-only files. It uses a pipeline operator (|) to send the files to the Rename-Item cmdlet, which renames the file. It uses the Passthru parameter of Rename-Item to send the renamed files to the Select-Object cmdlet, which selects the first 5 for display.

The Wait parameter of Select-Object prevents Windows PowerShell from stopping the Get-ChildItem cmdlet after it gets the first five read-only text files. Without this parameter, only the first five read-only files would be renamed.


PS C:\> Get-ChildItem *.txt -ReadOnly | Rename-Item -NewName {$_.BaseName + "-ro.txt"} -PassThru | Select-Object -First 5 -Wait

Related topics


Group-Object

Sort-Object

Where-Object



Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.