Exercise: Find Your Way
Explore Camp Bluejay's folders with Get-Location, Get-ChildItem, Set-Location and Get-Content, write six answers in answers.txt, and let a Pester check tell you which are right.
Novice · PowerShell · Operating System · Updated
A new camper at Camp Bluejay has questions: when do the canoes go out, what's in my cabin, did anyone find my shoe? The answers are all in the camp's folders. You'll find them with the commands from this chapter, write them 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-find-your-way, 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:
camp: the camp's folders and files, ready to explore. Don't change them.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.
What to do
Open PowerShell 7, go into the exercise folder with Set-Location, and answer each question with Get-Location, Get-ChildItem (with -File or -Directory when they help), Set-Location and Get-Content. 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)
Write each answer after its number, such as 1) Monday, and save the file. Open it with edit answers.txt, or in Notepad.
The six questions ask for a day in a schedule, how many files a cabin holds, which file is empty, a folder's name, a relative path between two folders, and the absolute path of one folder on your computer.
Check your work
The check uses Pester, PowerShell's test tool. Install it once:
Install-PSResource Pester
Then, from the exercise folder:
Invoke-Pester
At first all six 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 1 has no answer yet. Read camp\lake\dock\canoe-schedule.txt with Get-Content, 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
- Check where you are first. Before each answer, look at the prompt or run
Get-Location. Most wrong answers come from looking in the wrong folder. - Look without moving.
Get-ChildItemlists any folder whose path you give it, as in Looking Around: Get-ChildItem. - Read the columns. A folder has
din its Mode column; an empty file has a Length of0. - Test a path before you write it down. For the relative path, go to the starting folder and try your path with
Set-Location. If you arrive, it's right. Moving Around: Set-Location, .. and Tab shows how..climbs. - Let Tab type. Tab finishes folder and file names, and catches a typo before you press Enter.
When you're done
Every test is green, and Pester's last line says:
Tests Passed: 6, Failed: 0, Skipped: 0, Inconclusive: 0, NotRun: 0
Now look back at question 5. Could you have found the boathouse with an absolute path instead? Which would you rather type, and which would still work if you moved the whole camp folder somewhere else?
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.
On GitHub, click +, then New repository. Name it (for example
exercise-powershell-find-your-way), choose Public, and click Create repository. Leave out the README: your folder already has one.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-find-your-way.git git push -u origin mainIf 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-find-your-way.git git add . git commit -m "My solution" git push -u origin mainPaste your repo's address, such as
https://github.com/your-name/exercise-powershell-find-your-way, 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
Finding where things are, and reading what's in them, is half of every job you'll do in a terminal. You've now done it with no mouse at all.