Exercise: Pack for the Trip

Get a lake trip ready with PowerShell, unpacking a friend's zip, packing the trip's photos into one, checking three things with Test-Path and making a junction, and let a Pester check tell you what's done.

Apprentice · PowerShell · Operating System · Updated

The lake trip is on Friday. Riley sent a zip of photos, the trip's own photos need packing into one zip to share, there are a few things to check before you leave, and you'd like a short name for the trip folder. Everything you need is in this chapter. You'll do each job in PowerShell, and a check tells you which are done.

Get the exercise code

Open this lesson's GitHub repository, github.com/ask-codey/exercise-powershell-pack-for-the-trip, also linked from the Exercise box beside the lesson. Click Code, then Download ZIP, and unzip it into your terminal-practice folder. (Now you know a second way: Expand-Archive.) Or clone it with Git.

The folder holds:

  • trip: the trip's photos and documents.
  • inbox\from-riley.zip: Riley's photos, still zipped.
  • outbox: where the zip you pack goes.
  • answers.txt: one line for each question, for your answers.
  • Check.Tests.ps1: the check. You don't need to change it.

What to do

Open PowerShell 7 and go into the exercise folder with Set-Location. The steps are in the folder's README.md:

  1. Unpack Riley's zip into trip\photos\from-riley with Expand-Archive.
  2. Pack the whole trip\photos folder, Riley's photos included, into outbox\trip-photos.zip with Compress-Archive.
  3. Answer three questions with Test-Path: is a file there, is something a file or a folder, and is there any PDF at all.
  4. Make a junction, current-trip, that points at the trip folder, then answer two questions from its row in Get-ChildItem.

Write each answer after its number in answers.txt, such as 1) True, and save the file. Open it with edit answers.txt, or in Notepad.

# Write each answer after its number, on the same line. See README.md for the questions.
1)
2)
3)
4)
5)

Check your work

The check uses Pester, PowerShell's test tool. Install it once, if you haven't already:

Install-PSResource Pester

Then, from the exercise folder:

Invoke-Pester

At first all seven tests fail. That's expected: each step you finish turns its test green. A failing test says what it was looking for, for example Expected $true, because step 1 unpacks inbox\from-riley.zip with Expand-Archive into trip\photos\from-riley, but got $false.

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

  • Unpack before you pack. The zip should hold all six photos, so Riley's need to be in trip\photos first. Packed too early? Pack again with -Force.
  • Check the zip without unpacking over anything. Unpack it into a new folder, such as check-zip, and list it with Get-ChildItem -Recurse. Then remove that folder.
  • Ask the narrower question. For question 2, try -PathType Leaf and -PathType Container; the one that answers True is your answer.
  • Give the junction a full path. "$PWD\trip" is one: $PWD (read "dollar P W D") holds the path of the folder you're in. On a Mac or Linux, make a symbolic link instead.

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 at your current-trip row once more. If you moved the whole exercise folder somewhere else, would the junction still work? What would you run to point it at the new place?

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. The folder's .gitignore leaves current-trip out: a link only makes sense on your own computer.

  1. On GitHub, click +, then New repository. Name it (for example exercise-powershell-pack-for-the-trip), 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-pack-for-the-trip.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-pack-for-the-trip.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-pack-for-the-trip, 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

You've packed, unpacked, checked and linked a real folder with no mouse at all: the same four moves you'll use to send a project, back up a folder, or check a script's work.

Key terms

Related