Exercise: A Morning Start-Up Script

Write morning.ps1, a script that greets whoever runs it, shows the date, reads the to-do list beside it from any folder and shows the free space on drive C, answer three questions about running it, and let a Pester check run it the way someone else would.

Apprentice · PowerShell · Updated

Your first real application: a script you'll actually run tomorrow morning. It says hello, tells you the date, shows your to-do list and checks the free space on your drive, all from one line, the same way every day. It's small, but it has everything a bigger program has: input, work, output, a note that says what it does, and a way to start it from anywhere. A Pester check runs it the way someone else would: from another folder, as another user, with a to-do list of its own.

Get the exercise code

Open this lesson's GitHub repository, github.com/ask-codey/exercise-powershell-a-morning-start-up-script, 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:

  • morning.ps1: your script. For now it holds only a comment with the jobs.
  • todo.txt: a sample to-do list, the file your script reads. It stays beside the script.
  • answers.txt: three questions about running your script, each with a numbered line for your answer.
  • Check.Tests.ps1: the check. You don't need to change it.

What to do

Open PowerShell 7 in the exercise folder, the one with answers.txt in it, and open the script in VS Code with code .\morning.ps1 (Writing Scripts in VS Code), or in edit. Replace the starter's comment with your own script, one job at a time:

  1. A comment at the top that says what the script does (Your First Script).
  2. A greeting for whoever runs it, with $env:USERNAME, not your own name typed in.
  3. Today's date, with the day's name, from Get-Date -Format.
  4. The to-do list in todo.txt, the file beside the script. It must work from any folder, so use $PSScriptRoot (Run It from Anywhere).
  5. The free space on drive C, in GB, from Get-PSDrive. On a Mac or Linux, the drive is /.

Save, and run it with .\morning.ps1 after each job. When all five work, go to your home folder with Set-Location ~ and run the script by its full path: the list should still be there. Then answer the three questions in answers.txt: how to run the script from its own folder, how to put a Scripts folder on your PATH, and what a desktop shortcut's target should be.

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 every test fails. 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 the script couldn't find todo.txt. A script runs in the folder you're in, so name the file with the script's own folder. The check runs a copy of your script in a throwaway folder: it never changes your files.

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. From the exercise folder, run Get-ChildItem -Recurse | Unblock-File once, then Invoke-Pester again (Why a Script Won't Run: The Execution Policy).

Hints

  • Not recognized? In the script's own folder, the name needs .\ in front.
  • The list is missing when you run it from your home folder? A plain todo.txt means the folder you're in. "$PSScriptRoot\todo.txt" is the list beside the script.
  • A long number for the free space? Divide by 1GB, then [math]::Round( , 1).
  • A red wavy line in VS Code? Point at it and read the message: a missing quote is the usual suspect.

When you're done

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

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

Now make it part of your day. Copy morning.ps1 and todo.txt into a Scripts folder, add the folder to your PATH in your profile, and morning works in every window. Make a desktop shortcut to it, and it's one double-click away. What would you add next: the weather, your calendar, a quote for the day?

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-a-morning-start-up-script), 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-a-morning-start-up-script.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-a-morning-start-up-script.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-a-morning-start-up-script, 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 wrote an application, gave it a note that explains it, and made it work from any folder: the same steps behind every script people rely on at work, from a help desk's setup script to a developer's build.

Key terms

Related