Exercise: The Ten Biggest Files
Find out what's filling a full laptop's 24 made-up files with the pipeline (a count, the ten biggest, the big videos, the files of each type and a table of sizes in KB, each saved as a report) and rename its screenshots with ForEach-Object, while a Pester check tells you which answers are right.
Apprentice · PowerShell · Operating System · Updated
A cousin's old laptop is full, and before anyone deletes anything, you've been asked what's taking up the space. That's the job an IT help-desk tech gets every week, and the pipeline answers it in a handful of lines. You'll answer six questions about a copy of the laptop's files, save each answer as a small report, and a check tells you which answers are right.
Get the exercise code
Open this lesson's GitHub repository, github.com/ask-codey/exercise-powershell-the-ten-biggest-files, also linked from the Exercise box beside the lesson. Click Code, then Download ZIP, and unzip it into your terminal-practice folder. Or clone it with Git.
The folder holds:
old-laptop: the laptop's 24 made-up files in five folders, about 7 MB in all. They hold only the letter x, over and over, so each has a real size but nothing to open.README.md: the six tasks.Check.Tests.ps1: the check that reads your reports and looks at thePicturesfolder. You don't need to change it.
What to do
Open PowerShell 7 in the exercise folder, the one with Check.Tests.ps1 in it, and look around first:
Get-ChildItem old-laptop
Directory: C:\Users\Sam\terminal-practice\exercise-powershell-the-ten-biggest-files\old-laptop
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 9/14/2026 6:30 PM Documents
d---- 9/14/2026 6:30 PM Downloads
d---- 9/14/2026 6:30 PM Music
d---- 9/14/2026 6:30 PM Pictures
d---- 9/14/2026 6:30 PM Videos
Your dates will be the day you unzipped it. Then do the six tasks in the folder's README.md. Run every command from the exercise folder, and save every report there too, beside Check.Tests.ps1, not inside old-laptop:
- How many files? The number of files in
old-laptopand every folder inside it, incount.txt. - The ten biggest. Their names, biggest first, one a line, in
biggest.txt. - The big videos. The names of the
.mp4files over 500 KB, smallest first, inbig-videos.txt. - What kinds of files? Each extension's Count and Name, the biggest group first, in
types.txt. - Sizes people can read. The ten biggest as a table with
Nameand a column of your own,SizeKB, rounded to whole kilobytes, insizes.txt. - Tidy the screenshots. Every
Screenshot_file inold-laptop\Picturesrenamed to start withscreenshot-, with one ForEach-Object, rehearsed with-WhatIffirst.
Check your work
The check uses Pester, PowerShell's test tool. If you haven't installed it yet, install it once:
Install-PSResource Pester
Then, from the exercise folder:
Invoke-Pester
At first all six tests fail. That's expected: each task you finish turns its test green. A failing test says which task it is, what it expected and what it found. If test 3 says it found concert-clip.mp4 too, read the hints below.
Made a mistake? Write the report again: Set-Content and Out-File replace what a file held, as Writing to Files: Set-Content, Add-Content and > showed. To start over, download the exercise again.
If PowerShell says the check can't be loaded because running scripts is disabled or the file is not digitally signed, Windows has marked the downloaded files as coming from the internet. From the exercise folder, run Get-ChildItem -Recurse | Unblock-File once, then Invoke-Pester again. Why a Script Won't Run: The Execution Policy, in Module 5, explains what that does.
Hints
- One step at a time. Build each pipeline the way The Pipeline: | does: run the first command, look, add the next step. Save it to a file only when the screen shows the right answer.
- Only the names.
Select-Object -ExpandProperty Namegives names alone, one a line. A plainSelect-Object Namewrites a table with a heading, as Picking Columns: Select-Object shows. - Sort first, then take the top ten. Sorting: Sort-Object shows why the order of the steps matters.
- 500 KB is not 500,000. PowerShell's
KBis 1,024 bytes, so500KBis 512,000 bytes. Filtering: Where-Object and Comparisons has the comparisons. - Only the count and name of each group. Counting and Grouping: Measure-Object and Group-Object shows the parameter that leaves out the files.
- Round to whole kilobytes. Columns You Make Yourself: Calculated Properties shows
[math]::Round; with no second number, it rounds to a whole number. -WhatIfinside the braces. Doing Something to Each: ForEach-Object shows where it goes.
When you're done
Every test is green, and Pester's last line says:
Tests Passed: 6, Failed: 0, Skipped: 0, Inconclusive: 0, NotRun: 0
Now look at types.txt and sizes.txt side by side. Which kind of file would you look at first, if you had to free up space on that laptop?
Submit your exercise
The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here.
On GitHub, click +, then New repository. Name it (for example
exercise-powershell-the-ten-biggest-files), choose Public, and click Create repository. Leave out the README: your folder already has one.In a terminal in your exercise folder (PowerShell works), send your work to your new repo. If you downloaded the ZIP:
git init -b main git add . git commit -m "My solution" git remote add origin https://github.com/your-name/exercise-powershell-the-ten-biggest-files.git git push -u origin mainIf you cloned the starter instead, point your copy at your own repo, then commit and push:
git remote set-url origin https://github.com/your-name/exercise-powershell-the-ten-biggest-files.git git add . git commit -m "My solution" git push -u origin mainPaste your repo's address, such as
https://github.com/your-name/exercise-powershell-the-ten-biggest-files, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.
Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.
So what
"What's filling this drive?" is a question you'll be asked, and now you can answer it with facts: how many files, the biggest, and which kind takes the most room.