The Select-Object cmdlet allows you to select specific properties of a PowerShell object. By default, PowerShell commands do not display all properties, so you can use this command to display additional properties.
Different PowerShell objects can have different properties (attributes). Use the Get-Member cmdlet to list all available attributes for an object. For example, you want to display all available properties of a Windows Service object. You can use the Get-Service cmdlet to list services on Windows:
Get-Service| Get-Member -MemberType *Property*
If you know the names of the properties of the object, you will be able to select only the properties you need.
Get-Service | Select-Object Name, DisplayName, Status, StartType
You can display all the properties of any object:
Get-process -Name winword| Select-Object -Property *
You can display all of the attributes except for those that are specified with the ExcludeProperty option:
Get-Process-Name winword| Select-Object * -ExcludeProperty PrivilegedProcessorTime, PeakVirtualMemorySize, SafeHandle
The First and Last parameters allow you to get the first or last few elements in an array of objects. These parameters are most commonly used in conjunction with the Sort-Object cmdlet:
Get-Process | sort-Object WorkingSet –Descending| Select-Object Name,Path,MaxWorkingSet -First 5
With the Skip/SkipLast options, you can skip several objects at the beginning or end of an array:
Get-Process| Select-object -Skip 10
The Unique parameter allows you to select only unique elements in the array of objects. For example, even though multiple msedge prosedge are running on the computer, the following command will return only one unique process:
Get-Process msedge| Select-Object –Unique
Hint. Instead of using the full Select-Object command, you can also use its shorter alias Select.
The Select-Object cmdlet has an ExpandProperty parameter that allows you to expand details about a particular object property. This option is useful when an object property contains multiple values.
Note. Check how to use Grep Equivalent Select-String in PowerShell.
The ServicesDependedOn property of the service contains four additional values in this example. To get a list of them, run:
Get-service wlansvc|Select-Object -ExpandProperty ServicesDependedOn
ExpandProperty is also used when you need to get the value of a property of an object as a string:
Get-Process | sort-Object WorkingSet –Descending| Select-Object -ExpandProperty Name -First 5
You can use Select-Object to add a calculated property to the output of the command. The following construct is used to get the calculated property in the cmdlet:
@{ Name = ''; Expression = {}}
- The value in the Name field specifies the new property name.
- The Expression parameter contains the PowerShell code block (script block) that you want to run.
For example, you want to display the size of the Working Set of Windows processes in MB:
Get-Process| Select-Object -Property ProcessName,ID,@{ Name = 'RAM_Usage(MB)'; Expression = { ($_.WorkingSet/1MB) }}|sort-object "RAM_Usage(MB)" –Descending
This command lists running processes with their memory usage in MB.
We created a new calculated value and displayed it on the screen using Select-Object.
Note. The Select-Object cmdlet is often used with the Where-Object cmdlet.