Exercise: Sort the Downloads Folder
Sort a messy downloads folder of eighteen made-up files into photos, receipts, school, documents and a backup with New-Item, Copy-Item, Move-Item, Rename-Item and wildcards, and let a Pester check tell you which tasks are done.
Apprentice · PowerShell · Operating System · Updated
Everybody's Downloads folder ends up the same way: photos, receipts, two copies of a class schedule and an installer from who knows when, all in one heap. This one has eighteen files. You'll sort it into folders with the commands from this chapter, rehearsing each move first, and a check tells you which tasks are done.
Get the exercise code
Open this lesson's GitHub repository, github.com/ask-codey/exercise-powershell-sort-the-downloads-folder, 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:
downloads: eighteen made-up files to sort. They're empty: only their names matter.README.md: the six tasks.Check.Tests.ps1: the check that looks at thedownloadsfolder. You don't need to change it.
What to do
Open PowerShell 7, go into the exercise's downloads folder with Set-Location, and look around first:
Get-ChildItem -Name
beach-sunset.jpg
beach-volleyball.jpg
bike-shop-receipt.pdf
birthday-cake.png
class-schedule (1).pdf
class-schedule.pdf
dog-in-the-snow.jpg
game-setup.exe
graduation.png
grocery-receipt.pdf
lease-agreement.pdf
phone-repair-receipt.pdf
receipt-concert-tickets.pdf
recipe-banana-bread.docx
resume.docx
scan-1.pdf
scan-2.pdf
unfinished-download.tmp
Then do the six tasks in the folder's README.md, in order. Every folder you make goes inside downloads:
- Photos: every
.jpgand.pngin aphotosfolder. - Receipts: every file with
receiptin its name in areceiptsfolder. - School:
class-schedule.pdfinschool\fall-2026, made with one command. - The old copy:
class-schedule (1).pdfrenamedclass-schedule-old.pdf, inschool\fall-2026too. - Documents: every
.pdfand.docxstill at the top in adocumentsfolder.game-setup.exeandunfinished-download.tmpstay where they are. - Backup: a copy of the whole
photosfolder in abackupfolder, with the photos still inphotos.
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 (the one with Check.Tests.ps1 in it, one level above downloads):
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 to do, and what it found. Test 1 starts Expected strings to be the same, because task 1 moves every .jpg and .png into downloads\photos, and ends But was: '(no photos folder)'.
Nothing here is real, so a mistake is fixed with the same commands: move a file back, or rename it again. 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
- Rehearse every wildcard. List the pattern with
Get-ChildItemfirst, or add-WhatIfto the move, and read the matches, as in Wildcards and Filters. - Folders match too. Once there's a
receiptsfolder,*receipt*matches it as well. Make the pattern end with what the files end with. - One command, a whole path. New-Item makes every missing folder along a path, as Making Folders and Files: New-Item shows.
- Quotes for spaces. A name with a space needs quotes, and Tab adds them. Move-Item can rename on the way, as in Renaming: Rename-Item.
- Copy a folder with what's in it. Copy and Move: Copy-Item and Move-Item shows the parameter Copy-Item needs for that.
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 back at task 5. Which pattern did you use, and what would it have matched if you'd done task 5 before tasks 2 to 4?
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-sort-the-downloads-folder), 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-sort-the-downloads-folder.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-sort-the-downloads-folder.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-sort-the-downloads-folder, 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
Sorting a folder is a job you'll do for the rest of your life. You've now done it with a handful of commands, each one checked before it ran.