# 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 November 10, 2026

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](https://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 the `downloads` folder. 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:

```powershell
Get-ChildItem -Name
```

```text
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`:

1. **Photos:** every `.jpg` and `.png` in a `photos` folder.
2. **Receipts:** every file with `receipt` in its name in a `receipts` folder.
3. **School:** `class-schedule.pdf` in `school\fall-2026`, made with one command.
4. **The old copy:** `class-schedule (1).pdf` renamed `class-schedule-old.pdf`, in `school\fall-2026` too.
5. **Documents:** every `.pdf` and `.docx` still at the top in a `documents` folder. `game-setup.exe` and `unfinished-download.tmp` stay where they are.
6. **Backup:** a copy of the whole `photos` folder in a `backup` folder, with the photos still in `photos`.

## Check your work

The check uses Pester, PowerShell's test tool. If you haven't installed it yet, install it once:

```powershell
Install-PSResource Pester
```

Then, from the exercise folder (the one with `Check.Tests.ps1` in it, one level above `downloads`):

```powershell
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-ChildItem` first, or add `-WhatIf` to the move, and read the matches, as in *Wildcards and Filters*.
- **Folders match too.** Once there's a `receipts` folder, `*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:

```text
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.

1. 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.
2. In a terminal in your exercise folder (PowerShell works), send your work to your new repo. If you downloaded the ZIP:

   ```powershell
   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 main
   ```

   If you cloned the starter instead, point your copy at your own repo, then commit and push:

   ```powershell
   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 main
   ```

3. Paste 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.

## Key terms

- [New-Item](https://www.ask-codey.com/glossary/new-item)
- [Copy-Item](https://www.ask-codey.com/glossary/copy-item)
- [Move-Item](https://www.ask-codey.com/glossary/move-item)
- [Rename-Item](https://www.ask-codey.com/glossary/rename-item)
- [Wildcard](https://www.ask-codey.com/glossary/wildcard-powershell)
- [-WhatIf and -Confirm](https://www.ask-codey.com/glossary/whatif-and-confirm)

## Related

- [Making Folders and Files: New-Item](https://www.ask-codey.com/lessons/powershell-new-item)
- [Copy and Move: Copy-Item and Move-Item](https://www.ask-codey.com/lessons/powershell-copy-and-move)
- [Renaming: Rename-Item](https://www.ask-codey.com/lessons/powershell-rename-item)
- [Wildcards and Filters](https://www.ask-codey.com/lessons/powershell-wildcards-and-filters)

Source: https://www.ask-codey.com/lessons/exercise-powershell-sort-the-downloads-folder
