PowerShell Logo Small

Select-Object



This is the built-in help made by Microsoft for the command 'Select-Object', in PowerShell version 5 - as retrieved from Windows version 'Microsoft Windows Server 2012 R2 Standard' PowerShell help files on 2016-06-23.

For PowerShell version 3 and up, where you have Update-Help, this command was run just before creating the web pages from the help files.

SYNOPSIS

Selects objects or object properties.

SYNTAX


Select-Object [[-Property] [<Object[]>]] [-ExcludeProperty [<String[]>]] [-ExpandProperty [<String>]] [-InformationAction {SilentlyContinue | Stop | Continue | Inquire |
Ignore | Suspend}] [-InformationVariable [<System.String]>]] [-InputObject [<PSObject>]] [-Last [<Int32>]] [-Unique] [-Wait] [<CommonParameters>]
Select-Object [-Index [<Int32[]>]] [-InformationAction {SilentlyContinue | Stop | Continue | Inquire | Ignore | Suspend}] [-InformationVariable [<System.String]>]]
[-InputObject [<PSObject>]] [-Unique] [-Wait] [<CommonParameters>]
Select-Object [-InformationAction {SilentlyContinue | Stop | Continue | Inquire | Ignore | Suspend}] [-InformationVariable [<System.String]>]] [-SkipLast [<System.Int32]>]]
[<CommonParameters>]



Search powershellhelp.space

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.



<

RELATED LINKS

Online Version: http://go.microsoft.com/fwlink/p/?linkid=294007
Group-Object
Sort-Object
Where-Object

REMARKS

<

Examples


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

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



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










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

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



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.






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

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



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.






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

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

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



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.






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

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

a
b
c



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






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

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



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.






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

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



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.






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

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



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.