Exercise: Today's Weather Report
Answer a running club's three morning questions (will it rain, how warm will it be, when can we run dry) from saved copies of a real weather API's answers, with Invoke-RestMethod's objects, query parameters and the pipeline, saving each answer as a small report that a Pester check reads.
Journeyman · PowerShell · Updated
A running club meets at Green Lake in Seattle, and every morning someone asks the group chat the same three things: will it rain, how warm will it be, and when is the best time to run? A weather API answers all three, and with PowerShell you can turn its answer into a report in a handful of lines. This is the everyday shape of a lot of work: an office manager's morning numbers, a developer's status check, an analyst's daily download. You'll write seven small reports, and a check tells you which are right.
Get the exercise code
Open this lesson's GitHub repository, github.com/ask-codey/exercise-powershell-todays-weather-report, 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:
saved: three real answers from the National Weather Service's API, saved on 9 November 2026:points.jsonfor the spot by the lake,forecast.jsonwith the week's 14 forecast periods, andforecast-celsius.json, the same forecast in Celsius.README.md: the seven tasks.Check.Tests.ps1: the check that reads your reports. You don't need to change it.
Why saved copies
A forecast changes every hour, so a check can't know today's right answers. The saved copies are the answers the API gave when the exercise was made, and they turn into the same objects Invoke-RestMethod gave then. The exercise and its check work without the internet:
(Get-Content saved\forecast.json -Raw | ConvertFrom-Json).properties.periods | Select-Object -First 2 name, temperature, shortForecast
name temperature shortForecast
---- ----------- -------------
Overnight 43 Cloudy
Monday 56 Cloudy then Light Rain
Get-Content -Raw reads the whole file as one piece of text, and ConvertFrom-Json turns it into objects, as JSON: ConvertFrom-Json and ConvertTo-Json showed. When you're online, the README shows the same line on today's live forecast.
What to do
Open PowerShell 7 in the exercise folder, the one with Check.Tests.ps1 in it. Run every command from there, and save every report there too, not inside saved. Then do the seven tasks in the folder's README.md:
- Where's the forecast? The forecast address from
saved\points.json, inforecast-url.txt. - The next few hours in one line. The first period's
detailedForecast, intoday.txt. - The week at a glance. Every period's
name,temperatureandshortForecastas a table, inweek.txt. - When can the club run dry? The names of the periods with less than a 20% chance of rain, in order, in
dry.txt. - The warmest. The warmest period's name, in
warmest.txt. - Ask for Celsius. The forecast address with the query parameter
units=si, incelsius-url.txt. - The warmest, in Celsius. That period's temperature from
saved\forecast-celsius.json, incelsius.txt.
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 seven 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 report again: Set-Content and Out-File replace what a file held. To start over, download the exercise again.
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
- Dots go down a level at a time.
.properties.forecastin the points answer,.properties.periodsin the forecast, as APIs as Objects: Invoke-RestMethod shows. - Only the values.
Select-Object -ExpandProperty namegives names alone, one a line;Set-Contentsaves them. - A table goes into a file with
Out-Fileor>.Set-ContentafterSelect-Objectwrites@{name=…}lines instead, as Tables and Lists: Format-Table and Format-List explains. - A property inside a property needs braces. The chance of rain is
$_.probabilityOfPrecipitation.value, insideWhere-Object { … }, as Filtering: Where-Object and Comparisons shows. - Sort first, then take the first one. Sorting: Sort-Object shows why the order of the steps matters.
- Query parameters go after a
?. APIs as Objects: Invoke-RestMethod shows how to write one, and why the whole address goes in quotes.
When you're done
Every test is green, and Pester's last line says:
Tests Passed: 7, Failed: 0, Skipped: 0, Inconclusive: 0, NotRun: 0
Now look at dry.txt beside week.txt. If you were in the club, which morning would you pick for the long run?
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-todays-weather-report), 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-todays-weather-report.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-todays-weather-report.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-todays-weather-report, 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 weather report, a price list, a status page: once an API's answer is objects, a morning report is a handful of lines, and the same lines work again tomorrow on tomorrow's data.