Sorting: Sort-Object

Sort-Object puts objects in order by any property, smallest or A first unless you add -Descending, and with Select-Object -First after it gives you the top ten of anything, such as the ten biggest files on a drive; -Unique lists each value once.

Apprentice · PowerShell · Operating System · Updated

After this lesson you can

  • Sort objects by any property with Sort-Object, and reverse the order with -Descending
  • Find the top or bottom items with Sort-Object and Select-Object -First, and list each value once with -Unique

Key takeaways

  • Sort-Object Length puts files in order by size, smallest first. -Descending turns the order around.
  • Sort-Object Length -Descending | Select-Object -First 10 gives the ten biggest. Sort first, then take the top.
  • Numbers sort by their value and text sorts letter by letter. Give two properties, and the second one breaks ties in the first.
  • -Unique keeps each value once, which answers questions like "which kinds of files are on this drive?"

"What are the ten biggest files on my computer?" It's the first thing to ask when a drive fills up, and the first thing an IT tech checks before an upgrade. "Who are our top ten customers?" and "which songs did I play most?" are the same question about different things. Each is two steps: put everything in order, then take the top. File Explorer can order one folder; PowerShell can order everything a pipeline hands it.

This lesson uses the drive folder from The Pipeline: |. Run that lesson's practice-drive lines first if you need to, then Set-Location ~\terminal-practice\drive.

Put things in order: Sort-Object

Name the property to sort by, and Sort-Object puts the objects in order by it, smallest first:

Get-ChildItem -Recurse -File | Sort-Object Length | Select-Object Name, Length
Name                      Length
----                      ------
shopping-list.txt             35
resume.docx                36864
budget-2026.xlsx           49152
recipe-screenshot.png     133120
ringtone.mp3              153600
tax-return-2025.pdf       430080
birthday-cake.png         532480
class-notes.pdf           624640
cat-and-the-cucumber.mp4  716800
beach-sunset.jpg          870400
dog-in-the-snow.jpg       983040
unfinished-download.tmp  1126400
rain-sounds.mp3          1331200
birthday-party.mp4       1536000
road-trip-mix.mp3        1740800
phone-backup.zip         1945600
game-setup.exe           2355200
graduation.mp4           2867200

The order is called ascending: smallest first, or A to Z for text. Any property works: Sort-Object Name, Sort-Object LastWriteTime (oldest first), Sort-Object Extension.

Biggest first: -Descending, then the top ten

-Descending turns the order around: biggest first, Z to A, newest first. Then Select-Object -First 10, from Picking Columns: Select-Object, keeps the top ten:

Get-ChildItem -Recurse -File | Sort-Object Length -Descending | Select-Object -First 10 Name, Length
Name                      Length
----                      ------
graduation.mp4           2867200
game-setup.exe           2355200
phone-backup.zip         1945600
road-trip-mix.mp3        1740800
birthday-party.mp4       1536000
rain-sounds.mp3          1331200
unfinished-download.tmp  1126400
dog-in-the-snow.jpg       983040
beach-sunset.jpg          870400
cat-and-the-cucumber.mp4  716800

Read it aloud: "every file, sorted by Length, biggest first, then the first ten." The order of the steps matters. Select-Object -First 10 | Sort-Object Length would take the first ten files Get-ChildItem happens to find, and sort only those. Sort first, then take the top. For the bottom ten, the smallest, leave out -Descending.

Text, numbers and ties

Numbers sort by their value, so 9 comes before 10. Text sorts letter by letter, ignoring capital letters, the way a dictionary does. That's why the same three values sort differently as text:

'10', '9', '100' | Sort-Object
10, 9, 100 | Sort-Object
10
100
9
9
10
100

As text, '100' comes before '9' because its first letter, 1, comes before 9. A file's Length is a number, so sizes sort the right way.

When two objects have the same value, give a second property to break the tie. Here the photos and songs are sorted by Extension, and inside each extension by Name:

Get-ChildItem photos, music -File | Sort-Object Extension, Name | Select-Object Extension, Name
Extension Name
--------- ----
.jpg      beach-sunset.jpg
.jpg      dog-in-the-snow.jpg
.mp3      rain-sounds.mp3
.mp3      ringtone.mp3
.mp3      road-trip-mix.mp3
.png      birthday-cake.png
.png      recipe-screenshot.png

Each value once: -Unique

Which kinds of files are on this drive? -ExpandProperty, from Picking Columns: Select-Object, turns each file into its extension. Sort-Object -Unique sorts those and keeps each one once:

Get-ChildItem -Recurse -File | Select-Object -ExpandProperty Extension | Sort-Object -Unique
.docx
.exe
.jpg
.mp3
.mp4
.pdf
.png
.tmp
.txt
.xlsx
.zip

Eighteen files, eleven kinds. The short name for Sort-Object is sort. Windows PowerShell 5.1 sorts the same way. On a Mac or Linux, sort runs the system's own sort program instead, so write Sort-Object there.

Try it. List the three smallest files on the drive. Then try Get-Process | Sort-Object WorkingSet64 -Descending | Select-Object -First 5 to see which five programs on your computer are using the most memory right now.

Check yourself

2 questions to try on the lesson page.

So what

"The top ten" of anything is two steps: sort by the fact you care about, then take the first ten. Sort-Object does the hard part, on any property of any object.

Key terms

Cheat sheets

Related