TExcelTAD
Home › PowerShell › PowerShell Where, Select & Sort

PowerShell Where, Select & Sort

Three cmdlets do most of the data work in PowerShell pipelines: Where-Object keeps the objects you want, Select-Object picks properties or a number of objects, and Sort-Object puts them in order. They work on any objects: files, processes, CSV rows, API results or your own custom objects.

We'll use this small inventory throughout the lesson:

powershell · try it yourself
$inventory = @(
    [pscustomobject]@{ Name = 'Laptop';   Category = 'Computers';   Price = 1200; Stock = 5 }
    [pscustomobject]@{ Name = 'Mouse';    Category = 'Accessories'; Price = 25;   Stock = 120 }
    [pscustomobject]@{ Name = 'Monitor';  Category = 'Displays';    Price = 300;  Stock = 0 }
    [pscustomobject]@{ Name = 'Keyboard'; Category = 'Accessories'; Price = 80;   Stock = 35 }
    [pscustomobject]@{ Name = 'Tablet';   Category = 'Computers';   Price = 450;  Stock = 12 }
)
$inventory | Format-Table

Name     Category    Price Stock
----     --------    ----- -----
Laptop   Computers    1200     5
Mouse    Accessories    25   120
Monitor  Displays      300     0
Keyboard Accessories    80    35
Tablet   Computers     450    12

Where-Object: filtering

Where-Object (alias where or ?) passes on only the objects for which the condition is true:

powershell · try it yourself
$inventory = @(
    [pscustomobject]@{ Name = 'Laptop';   Category = 'Computers';   Price = 1200; Stock = 5 }
    [pscustomobject]@{ Name = 'Mouse';    Category = 'Accessories'; Price = 25;   Stock = 120 }
    [pscustomobject]@{ Name = 'Monitor';  Category = 'Displays';    Price = 300;  Stock = 0 }
    [pscustomobject]@{ Name = 'Keyboard'; Category = 'Accessories'; Price = 80;   Stock = 35 }
    [pscustomobject]@{ Name = 'Tablet';   Category = 'Computers';   Price = 450;  Stock = 12 }
)

($inventory | Where-Object Stock -eq 0).Name
'---'
$inventory | Where-Object Category -like 'Comp*' | ForEach-Object Name
'---'
$inventory | Where-Object { $_.Price -lt 500 -and $_.Stock -gt 10 } | ForEach-Object Name
'---'
$inventory.Where({ $_.Price -gt 400 }, 'First').Name
Monitor
---
Laptop
Tablet
---
Mouse
Keyboard
Tablet
---
Laptop

The .Where() method on an array accepts a second argument: 'First', 'Last', 'Skip', 'Until' or 'Split', e.g. to stop at the first match.

Tip: Filter as early ("left") as possible. Get-ChildItem -Filter *.log is much faster than Get-ChildItem | Where-Object Name -like '*.log', because the file system does the work.

Select-Object: properties

Select-Object (alias select) creates new objects with only the properties you list. Add calculated properties with @{ Name = …; Expression = { … } } (N and E are accepted abbreviations):

powershell · try it yourself
$inventory = @(
    [pscustomobject]@{ Name = 'Laptop';   Price = 1200; Stock = 5 }
    [pscustomobject]@{ Name = 'Mouse';    Price = 25;   Stock = 120 }
    [pscustomobject]@{ Name = 'Keyboard'; Price = 80;   Stock = 35 }
)

$inventory | Select-Object Name, @{ N = 'Value'; E = { $_.Price * $_.Stock } }
$inventory | Select-Object -Property * -ExcludeProperty Stock

Name     Value
----     -----
Laptop    6000
Mouse     3000
Keyboard  2800
Laptop
Mouse
Keyboard

Select-Object: first, last, skip, unique

Select-Object also selects objects by position:

powershell · try it yourself
$scores = 88, 92, 75, 92, 61, 88, 99
($scores | Select-Object -First 3) -join ' '
($scores | Select-Object -Last 2) -join ' '
($scores | Select-Object -Skip 5) -join ' '
($scores | Select-Object -Unique) -join ' '
($scores | Select-Object -Index 0, 6) -join ' '
88 92 75
88 99
88 99
88 92 75 61 99
88 99

Sort-Object

Sort-Object sorts by one or more properties, ascending by default. Use -Descending, -Unique, -Top and -Bottom (PowerShell 7), or a hashtable to choose the direction per property:

powershell · try it yourself
$inventory = @(
    [pscustomobject]@{ Name = 'Laptop';   Category = 'Computers';   Price = 1200 }
    [pscustomobject]@{ Name = 'Mouse';    Category = 'Accessories'; Price = 25 }
    [pscustomobject]@{ Name = 'Monitor';  Category = 'Displays';    Price = 300 }
    [pscustomobject]@{ Name = 'Keyboard'; Category = 'Accessories'; Price = 80 }
    [pscustomobject]@{ Name = 'Tablet';   Category = 'Computers';   Price = 450 }
)

($inventory | Sort-Object Price).Name -join ', '
($inventory | Sort-Object Price -Descending -Top 2).Name -join ', '
$inventory |
    Sort-Object Category, @{ Expression = 'Price'; Descending = $true } |
    ForEach-Object { '{0,-12} {1,-9} {2,5}' -f $_.Category, $_.Name, $_.Price }
Mouse, Keyboard, Monitor, Tablet, Laptop
Laptop, Tablet
Accessories  Keyboard     80
Accessories  Mouse        25
Computers    Laptop     1200
Computers    Tablet      450
Displays     Monitor     300

Sorting strings and numbers

Sorting compares values by their type. Text that contains numbers sorts alphabetically, which is often not what you want; sort by a converted value instead:

powershell · try it yourself
$versions = '10', '9', '100', '2'
($versions | Sort-Object) -join ' '
($versions | Sort-Object { [int]$_ }) -join ' '

$files = 'log10.txt', 'log2.txt', 'log1.txt'
($files | Sort-Object { [int]($_ -replace '\D', '') }) -join ' '
'banana', 'Apple', 'cherry' | Sort-Object          # case-insensitive by default
10 100 2 9
2 9 10 100
log1.txt log2.txt log10.txt
Apple
banana
cherry

A script block given to Sort-Object acts as a calculated sort key.

Test yourself

Which command keeps only items whose Price is above 100?

  1. $items | Select-Object Price -gt 100
  2. $items | Where-Object Price -gt 100
  3. $items | Sort-Object Price 100
  4. $items | Where Price > 100

What does 1, 5, 3 | Sort-Object -Descending | Select-Object -First 1 return?

  1. 1
  2. 3
  3. 5
  4. 5, 3

Why does '10','9','2' | Sort-Object give 10, 2, 9?

  1. It's a bug
  2. The values are strings, so they sort alphabetically
  3. Sort-Object sorts randomly
  4. It sorts by length