Exercise: The Moving-Day List

Clear the hallway of a new apartment with Remove-Item and -WhatIf, write and add to its lists with Set-Content, Add-Content, > and >>, fix a settings file with edit, and read the ends of two long files, while a Pester check tells you which jobs are done.

Apprentice · PowerShell · Operating System · Updated

You've moved into apartment 4B. The movers left a broken vase and a pile of cardboard in the hallway, your smart speaker still has your old address, and the to-do list keeps growing. You'll do seven moving-day jobs with the commands from this chapter, and a check tells you which ones are done.

Get the exercise code

Open this lesson's GitHub repository, github.com/ask-codey/exercise-powershell-the-moving-day-list, 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:

  • apartment: the new apartment's files. This is where you work.
  • Check.Tests.ps1: the check that looks at the apartment folder. You don't need to change it.

What to do

Open PowerShell 7, go into the exercise folder with Set-Location, then into apartment. The jobs are in the folder's README.md, in this order:

  1. Throw out hallway\broken-vase.txt and hallway\bubble-wrap.txt. The spare key and the lamp stay.
  2. Get rid of the folder hallway\cardboard, with everything in it. The hallway itself stays.
  3. Make door-sign.txt, with one line: Apartment 4B.
  4. Add - water the plants to the end of todo.txt, keeping the three lines it has.
  5. Open speaker-settings.ini with edit, and change its last line to address=48 Maple Avenue, Apt 4B.
  6. Save the first three lines of packing-list.txt into a new file, unpack-first.txt, with one command.
  7. Add the last line of movers.log, which says where box 14 ended up, to the end of todo.txt, with one command.

Every new file goes inside apartment, next to todo.txt. Deleting can't be undone, but this folder is a download: if a job goes wrong, unzip a fresh copy and start again.

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, not apartment):

Invoke-Pester

At first all seven tests fail. That's expected: each job you do turns its test green. A failing test names its job and what it looked for, for example Expected path '…\apartment\hallway\cardboard' to not exist, because job 2 removes hallway\cardboard and the boxes in it, which needs -Recurse, but it did exist.

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 delete. Run it with -WhatIf first, as in Deleting Safely: Remove-Item, -WhatIf and -Confirm. A wildcard such as hallway\*.txt would take the spare key and the lamp too.
  • A full folder needs -Recurse. Without it, PowerShell stops and asks.
  • Replace or add? Set-Content and > replace a whole file; Add-Content and >> add to its end. Jobs 4 and 7 both add to todo.txt.
  • Two commands in one. Jobs 6 and 7 read part of a file and send it into another: Get-Content with -TotalCount or -Tail, then > or >>, all on one line, as in The Start and End of a File: -TotalCount, -Tail and -Wait and Writing to Files: Set-Content, Add-Content and >.
  • Look after every job. Get-Content shows what a file holds now, and Get-ChildItem hallway what's left in the hallway.

When you're done

Every test is green, and Pester's last line says:

Tests Passed: 7, Failed: 0, Skipped: 0, Inconclusive: 0, NotRun: 0

Now look back at job 5. Could you have made that change with Set-Content instead of an editor? What would you have had to type, and what could have gone wrong?

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-the-moving-day-list), 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:

    git init -b main
    git add .
    git commit -m "My solution"
    git remote add origin https://github.com/your-name/exercise-powershell-the-moving-day-list.git
    git push -u origin main
    

    If 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-moving-day-list.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-the-moving-day-list, 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

Clearing out, writing lists and fixing one line are the jobs you'll do in a terminal most often. You've now done all of them from the prompt, and rehearsed every delete before it happened.

Key terms

Related