Exercise: The Workshop's Machines

Mind an inventor's workshop with variables, quotes, dates, arrays, hashtables and environment variables, from counting her machines to oiling the gears by name from your PATH, while a Pester check reads the eight results.

Apprentice · PowerShell · Updated

Ada Wren is away at the inventors' fair, and you're minding her workshop. Her letter asks for a few things: count the machines, add a new one to the list, write up a parts order, work out when the gears need oil, tidy the list of where things are kept, and get the kettle-bot and the oil can working. Each job uses one idea from this chapter. Jobs like these are the everyday shape of scripting: a value kept in a variable, a line of text built from it, a date worked out, a setting a program reads. You'll do eight tasks, and a check tells you which are done.

Get the exercise code

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

  • letter.txt: Ada's note to you. Read it first.
  • machines.txt: her machines, one a line, and oil-log.txt: the date the gears were last oiled.
  • kettle-bot.ps1 and tools\oil.ps1: two of her machines, small scripts. Read each one before you run it.
  • README.md: the eight tasks.
  • Check.Tests.ps1: the check that reads your results. 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. Run every command from there, and write every result there too. Then do the eight tasks in the folder's README.md:

  1. The machine count. machines.txt read into $machines, and a line built from $machines.Count with $( ), in label.txt.
  2. A new machine. The music box added to $machines with +=, and the whole list in machines-new.txt.
  3. The parts order. Three lines with prices, dollar signs and all, written with a here-string, in order.txt.
  4. The next oiling. The date 30 days after the one in oil-log.txt, written year-month-day, in next-oiling.txt.
  5. Days until Ada is back. The days from the date in oil-log.txt to December 4, 2026, in days.txt.
  6. Where things are. A hashtable of drawers with one key changed and one added, and a line built from it, in drawers.txt.
  7. Tea. The kettle-bot run with power from an environment variable. It writes tea.txt itself.
  8. Oil. The tools folder put on your PATH for this window, and the oil can run by name. It writes oiled.txt itself.

Here are Ada's machines, so you know what you're running. With no power, the kettle-bot only complains:

.\kettle-bot.ps1
NO POWER

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 eight 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.

Made a mistake? Write the file again: Set-Content replaces what a file held. To start over, download the exercise again. What you set for tasks 7 and 8 lasts until you close the window, so in a new window, set it again before you run the machines.

If PowerShell says a script 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 try again. Why a Script Won't Run: The Execution Policy, in the next chapter, explains what that does.

Hints

  • Look before you write. Build each answer on the screen first, and send it to a file only when it looks right, as Variables: $name does with $drawings.
  • Which quotes? Double quotes fill in values; a single-quoted here-string keeps every $, as Text: Quotes and $( ) shows with the parts order.
  • A date from text. [datetime] turns the date in oil-log.txt into a date, and one date minus another has a .Days, as Numbers, Dates and Types shows.
  • Change or add. Setting a key that's already in a hashtable changes it; setting a new one adds it, as Lists and Lookups: Arrays and Hashtables shows.
  • Which box does the kettle-bot read? Its first lines say. Environment Variables and PATH shows the difference between $POWER and $env:POWER.
  • Which folder goes on PATH? The tools folder inside the exercise folder; Get-Location shows the exercise folder's full path.

When you're done

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

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

Now open a new PowerShell window in the exercise folder and type oil gears again. What happens, and why?

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-workshops-machines), 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-workshops-machines.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-workshops-machines.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-workshops-machines, 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

Variables, text, dates, lists, lookups and the environment are what every script is made of. You've now used each one on a real job, and the next chapters build scripts from them.

Key terms

Related