Select-Object chooses properties from objects flowing through the pipeline. Use it to expose useful columns, reduce noisy output, or prepare flexible data for later commands. section 8.7 section 20.1
Use Select-Object for several common pipeline tasks:
- Choose properties explicitly, such as process Name, ID, CPU, and PM: Get-Process | Select-Object Name,ID,CPU,PM. section 8.7
- Keep a positional subset with -First or -Last. These parameters select objects by position, not by criteria. section 8.7 section 12.5
- Extract property values with -ExpandProperty when a later parameter needs the values rather than the original objects. section 10.8
- Create calculated or renamed properties with hash tables containing Name and Expression keys. section 10.5
Trim process data for HTML
Get-Process | Select-Object -Property Name,ID,CPU,PM | ConvertTo-HTML | Out-File test.html Selecting properties prevents the HTML table from including every process property. The Select alias also works: Get-Process | Select Name,ID,CPU,PM. section 8.7
Understand the resulting object
Get-Process | Sort-Object CPU -Descending | Select-Object Name,ID,CPU Select-Object does not remove properties from the original process objects. It creates custom PSObject instances containing the selected properties, so their default formatting can differ from Get-Process output. section 8.8
Extract a resource-group name
Get-AzResourceGroup -Location westus2 | Select-Object -First 1 -ExpandProperty ResourceGroupName This returns the property value itself, allowing it to supply a parameter that expects a string instead of a resource-group object. section 10.8
Rename imported fields
Import-Csv .\newusers.csv | Select-Object -Property *, @{name='samAccountName';expression={$_.login}}, @{label='Name';expression={$_.login}}, @{n='Department';e={$_.Dept}} The asterisk preserves existing properties while calculated properties map CSV fields to names expected by a later command. section 10.5
Listing 20.1 Starting point: Get-DiskInventory.ps1
<#
.SYNOPSIS
Get-DiskInventory retrieves logical disk information from one or
more computers.
.DESCRIPTION
Get-DiskInventory uses CIM to retrieve the Win32_LogicalDisk
instances from one or more computers. It displays each disk's
drive letter, free space, total size, and percentage of free
space.
.PARAMETER computername
The computer name, or names, to query. Default: Localhost.
.PARAMETER drivetype
The drive type to query. See Win32_LogicalDisk documentation
for values. 3 is a fixed disk, and is the default.
.EXAMPLE
Get-DiskInventory -ComputerName SRV02 -drivetype 3
#>
param (
$computername = 'localhost',
$drivetype = 3
)
Get-CimInstance -class Win32_LogicalDisk -ComputerName $computername `
-filter "drivetype=$drivetype" |
Sort-Object -property DeviceID |
Select-Object -property DeviceID, #1
@{label='FreeSpace(MB)';expression={$_.FreeSpace / 1MB -as [int]}},
@{label='Size(GB)';expression={$_.Size / 1GB -as [int]}},
@{label='%Free';expression={$_.FreeSpace / $_.Size * 100 -as [int]}}
Inspect a parameterized disk-inventory script that selects object properties before callers choose table or CSV output. section 20.1
