Exercise: Your Toolkit, Version 1
Build the first version of your own PowerShell toolkit, a file with an alias, three small functions and a prompt in your colors, answer five questions about running scripts and installing tools, and let a Pester check try every shortcut for real.
Apprentice · PowerShell · Updated
Everyone who works in PowerShell every day ends up with a toolkit: a file of short names and small helpers collected over the years, and taken to every computer they use. IT techs carry theirs from PC to PC; developers keep theirs next to their projects. Today you start yours. Version 1 holds a short name that opens a folder in File Explorer, a jump to your practice folder, a folder-maker that goes straight in, a backup in three letters, and a prompt in colors you choose. A Pester check loads your toolkit, tries every shortcut for real and tells you which ones work.
Get the exercise code
Open this lesson's GitHub repository, github.com/ask-codey/exercise-powershell-your-toolkit-v1, 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:
toolkit\toolkit.ps1: your toolkit, with one example done and five jobs to do, each a comment saying what to add.answers.txt: five questions about running scripts and installing tools, each with a numbered line for your answer.Check.Tests.ps1: the check. You don't need to change it.
What to do
You need PowerShell 7 for this one: the prompt uses $PSStyle, which Windows PowerShell 5.1 doesn't have. Open it in the exercise folder, the one with answers.txt in it, and open the toolkit with edit .\toolkit\toolkit.ps1. Each job is a comment, with room under it for your lines:
# Example: which shows where a command comes from, the way Get-Command does.
Set-Alias -Name which -Value Get-Command
# 1. open: an alias for Invoke-Item, so that open . opens the folder you're in, in File Explorer.
open: an alias forInvoke-Item, the command that opens a file or folder the way a double-click does (Aliases and Small Functions).practice: a function that goes to your practice folder,$HOME\terminal-practice.mkcd: a function that makes a folder, and any folders on the way to it, then goes into it.mkcd "trip photos\day 1"must work, spaces and all.bak: a function that copies a file to the same name with.bakon the end, sobak notes.txtmakesnotes.txt.bak. It's the keep-a-copy habit from Your Profile, in three letters.- Your prompt: a
promptfunction with the name of the folder you're in, at least one$PSStylecolor,$PSStyle.Resetafter it, and>at the end (A Prompt in Your Colors). The colors are your choice.
Save, then load the toolkit into the window you're in and try each shortcut as you go: . .\toolkit\toolkit.ps1, then mkcd test-folder, then practice. Your new prompt appears straight away. Finally, answer the five questions in answers.txt, using Why a Script Won't Run: The Execution Policy, Your Profile and Installing Apps and Modules: winget and the PowerShell Gallery.
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 most tests fail. That's expected: each job you finish turns its test green. A failing test names the job and says what to look at, for example Expected 'Function', because job 4 is a function named bak, with param($Path) for the file's name. The check loads your toolkit into a session of its own and tries each shortcut in a throwaway folder: it never reads or changes your own profile.
If PowerShell says the check can't be loaded because it is not digitally signed, Windows has marked the downloaded files as coming from the internet. You know the fix now: from the exercise folder, run Get-ChildItem -Recurse | Unblock-File once, then Invoke-Pester again.
Hints
- Is the name free?
Get-Command openbefore you make it. Red text means nobody's using the name. - An alias is one command, with no values. Anything more is a function.
- A function that takes a word starts with
param($Path)and uses$Pathwhere the word goes."$Path.bak"is the same name with.bakon the end. - The prompt must give back text, as its last line, in double quotes.
Split-Path -Leaf (Get-Location)is the folder's name. Try your prompt at the prompt first, then copy the version you like into the toolkit. - An error when you load the toolkit? The message names the line: look there first.
When you're done
Every test is green, and Pester's last line says:
Tests Passed: 12, Failed: 0, Skipped: 0, Inconclusive: 0, NotRun: 0
Now make it yours for real. Keep a copy of your profile, then add one line to it that dot-sources your toolkit, with the path to where your folder is, such as . "$HOME\terminal-practice\exercise-powershell-your-toolkit-v1\toolkit\toolkit.ps1". Open a new window: your prompt and your shortcuts are there, and they will be in every window from now on. What would you add for version 2?
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-your-toolkit-v1), 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-your-toolkit-v1.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-your-toolkit-v1.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-your-toolkit-v1, 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
A toolkit file is how working developers and administrators make any PowerShell window feel like their own, and yours now has a first version you wrote, tested and can carry anywhere.