Exercise: Guess the Command

Answer eight questions from a bike shop's owner by guessing verb-noun commands and checking them with Get-Command, Get-Alias and Get-Help, write the answers in answers.txt, and let a Pester check tell you which are right.

Apprentice · PowerShell · Updated

The owner of Maple Street Bikes, a small bike shop, uses one laptop for everything: the till, the orders, the music in the shop. She's heard that PowerShell could help look after it, and she's left you eight questions. Each asks for a command, or a fact about one. You'll guess each command from its verb and noun, check the guess, write the answers in a file, and a check tells you which are right.

Get the exercise code

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

  • answers.txt: one line for each question, for your answers.
  • Check.Tests.ps1: the check that reads your answers. You don't need to change it.
  • README.md: the eight questions.

What to do

Open PowerShell 7 in the exercise folder: in File Explorer, right-click the folder and choose Open in Terminal, or type Set-Location followed by the folder's path. The questions are in the folder's README.md, and answers.txt has a line waiting for each one:

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

For each question, guess first, then check: Get-Command for a command's name, its noun or its verb, Get-Alias for a short name, Get-Help for how a command is used, and Tab and Ctrl+Space for its parameters. Write each answer after its number, such as 1) Get-Date, and save the file. Open it with edit answers.txt, or in Notepad.

The eight questions ask for three commands you can guess from what they do, the full name of a short name, how many commands share one noun, which module a command comes from, a parameter of Get-Help, and one whole command with a parameter and its value.

Every command you run for this exercise only looks. One question asks you to find a command that changes something: find it with Get-Command, and don't run it.

Check your work

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

Install-PSResource Pester

Then, from the exercise folder:

Invoke-Pester

At first all eight tests fail. That's expected: each answer you write turns its test green. A failing test names its question and says where to look, for example Expected a value, because question 3 has no answer yet. Get-Alias gsv shows what it runs, but got $null or empty.

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

  • Guess before you search. Say what you want to do and what to do it to, and put a dash between them. Verb-Noun: Names That Explain Themselves has the pattern.
  • Check without running. Get-Command looks a name up and never runs it, as Finding a Command: Get-Command shows. A guess that's "not recognized" is a guess to change, not a mistake.
  • Short names have long names. Short Names: Aliases shows how Get-Alias turns one into the other.
  • Count on your own computer. One answer is a number of rows that Get-Command shows you. The check counts on your computer too.
  • Try the whole command. For the last question, run your command at the prompt first, as in Parameters, Values and Switches. If it shows Windows Audio, write it down.

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 look back at your answers. How many of the commands did you guess right before you checked? And which of the eight would you write differently in a script?

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-guess-the-command), 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-guess-the-command.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-guess-the-command.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-guess-the-command, 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 found and checked commands you'd never been taught, with nothing but their names and PowerShell's own help. That's how people who work in PowerShell every day meet a new command.

Key terms

Related