Checking the Weather with PowerShell

Image Description

Daily PowerShell #31

Daily PowerShell

November 16, 2021

In this post, we’ll check the weather with PowerShell.

Weather.gov API

Weather.gov provides a free API for checking the weather. We can consume this API in PowerShell with Invoke-RestMethod. Some of the endpoints are listed below:

You can view the entire API documentation by visiting the OpenAPI docs.

Forecast

To display the weather forecast for an area, you’ll need the latitude and longitude.

For example, this is the current forecast for Hailey, Idaho.

$Result = Invoke-RestMethod 'https://api.weather.gov/points/43.5129009,-114.3315179'
(Invoke-RestMethod ($Result.Properties.Forecast)).Properties.periods

The resulting forecast looks like this for the next couple of days.

number           : 1
name             : Tonight
startTime        : 11/15/2021 6:00:00 PM
endTime          : 11/16/2021 6:00:00 AM
isDaytime        : False
temperature      : 27
temperatureUnit  : F
temperatureTrend : rising
windSpeed        : 8 to 15 mph
windDirection    : W
icon             : https://api.weather.gov/icons/land/night/rain_showers,60/snow,50?size=medium
shortForecast    : Rain Showers Likely then Chance Rain And Snow Showers
detailedForecast : Rain showers likely between 7pm and 1am, then a chance of rain and snow showers between 1am and 2am. Mostly cloudy. Low around 27, with temperatures rising to around 31 overnight. West wind 8 to 15   
                   mph, with gusts as high as 23 mph. Chance of precipitation is 60%.

number           : 2
name             : Tuesday
startTime        : 11/16/2021 6:00:00 AM
endTime          : 11/16/2021 6:00:00 PM
isDaytime        : True
temperature      : 38
temperatureUnit  : F
temperatureTrend : falling
windSpeed        : 12 to 16 mph
windDirection    : NW
icon             : https://api.weather.gov/icons/land/day/few?size=medium
shortForecast    : Sunny
detailedForecast : Sunny. High near 38, with temperatures falling to around 31 in the afternoon. Northwest wind 12 to 16 mph, with gusts as high as 24 mph.

number           : 3
name             : Tuesday Night
startTime        : 11/16/2021 6:00:00 PM
endTime          : 11/17/2021 6:00:00 AM
isDaytime        : False
temperature      : 13
temperatureUnit  : F
temperatureTrend : rising
windSpeed        : 6 to 12 mph
windDirection    : NNW
icon             : https://api.weather.gov/icons/land/night/few?size=medium
shortForecast    : Mostly Clear
detailedForecast : Mostly clear. Low around 13, with temperatures rising to around 16 overnight. North northwest wind 6 to 12 mph.

number           : 4
name             : Wednesday
startTime        : 11/17/2021 6:00:00 AM
endTime          : 11/17/2021 6:00:00 PM
isDaytime        : True
temperature      : 33
temperatureUnit  : F
temperatureTrend : 
windSpeed        : 3 to 7 mph
windDirection    : E
icon             : https://api.weather.gov/icons/land/day/few?size=medium
shortForecast    : Sunny
detailedForecast : Sunny, with a high near 33. East wind 3 to 7 mph.

number           : 5
name             : Wednesday Night
startTime        : 11/17/2021 6:00:00 PM
endTime          : 11/18/2021 6:00:00 AM
isDaytime        : False
temperature      : 18
temperatureUnit  : F
temperatureTrend : 
windSpeed        : 7 mph
windDirection    : ENE
icon             : https://api.weather.gov/icons/land/night/sct?size=medium
shortForecast    : Partly Cloudy
detailedForecast : Partly cloudy, with a low around 18. East northeast wind around 7 mph.

number           : 6
name             : Thursday
startTime        : 11/18/2021 6:00:00 AM
endTime          : 11/18/2021 6:00:00 PM
isDaytime        : True
temperature      : 40
temperatureUnit  : F
temperatureTrend : 
windSpeed        : 2 to 7 mph
windDirection    : ESE
icon             : https://api.weather.gov/icons/land/day/bkn/snow,20?size=medium
shortForecast    : Partly Sunny then Slight Chance Light Snow
detailedForecast : A slight chance of snow after 5pm. Partly sunny, with a high near 40. Chance of precipitation is 20%. Little or no snow accumulation expected.

number           : 7
name             : Thursday Night
startTime        : 11/18/2021 6:00:00 PM
endTime          : 11/19/2021 6:00:00 AM
isDaytime        : False
temperature      : 27
temperatureUnit  : F
temperatureTrend : 
windSpeed        : 6 mph
windDirection    : NNE
icon             : https://api.weather.gov/icons/land/night/snow,30/snow,40?size=medium
shortForecast    : Chance Light Snow
detailedForecast : A chance of snow. Mostly cloudy, with a low around 27. Chance of precipitation is 40%. New snow accumulation of less than one inch possible.

number           : 8
name             : Friday
startTime        : 11/19/2021 6:00:00 AM
endTime          : 11/19/2021 6:00:00 PM
isDaytime        : True
temperature      : 42
temperatureUnit  : F
temperatureTrend : 
windSpeed        : 8 mph
windDirection    : W
icon             : https://api.weather.gov/icons/land/day/snow,40/snow,30?size=medium
shortForecast    : Chance Light Snow
detailedForecast : A chance of snow. Partly sunny, with a high near 42. Chance of precipitation is 40%. Little or no snow accumulation expected.

number           : 9
name             : Friday Night
startTime        : 11/19/2021 6:00:00 PM
endTime          : 11/20/2021 6:00:00 AM
isDaytime        : False
temperature      : 24
temperatureUnit  : F
temperatureTrend : 
windSpeed        : 6 mph
windDirection    : NW
icon             : https://api.weather.gov/icons/land/night/snow,20/sct?size=medium
shortForecast    : Slight Chance Light Snow then Partly Cloudy
detailedForecast : A slight chance of snow before 11pm. Partly cloudy, with a low around 24. Chance of precipitation is 20%.

number           : 10
name             : Saturday
startTime        : 11/20/2021 6:00:00 AM
endTime          : 11/20/2021 6:00:00 PM
isDaytime        : True
temperature      : 40
temperatureUnit  : F
temperatureTrend : 
windSpeed        : 3 to 7 mph
windDirection    : WSW
icon             : https://api.weather.gov/icons/land/day/sct?size=medium
shortForecast    : Mostly Sunny
detailedForecast : Mostly sunny, with a high near 40.

number           : 11
name             : Saturday Night
startTime        : 11/20/2021 6:00:00 PM
endTime          : 11/21/2021 6:00:00 AM
isDaytime        : False
temperature      : 21
temperatureUnit  : F
temperatureTrend : 
windSpeed        : 7 mph
windDirection    : NW
icon             : https://api.weather.gov/icons/land/night/sct?size=medium
shortForecast    : Partly Cloudy
detailedForecast : Partly cloudy, with a low around 21.

number           : 12
name             : Sunday
startTime        : 11/21/2021 6:00:00 AM
endTime          : 11/21/2021 6:00:00 PM
isDaytime        : True
temperature      : 40
temperatureUnit  : F
temperatureTrend : 
windSpeed        : 3 to 7 mph
windDirection    : W
icon             : https://api.weather.gov/icons/land/day/sct?size=medium
shortForecast    : Mostly Sunny
detailedForecast : Mostly sunny, with a high near 40.



number           : 13
name             : Sunday Night
startTime        : 11/21/2021 6:00:00 PM
endTime          : 11/22/2021 6:00:00 AM
isDaytime        : False
temperature      : 23
temperatureUnit  : F
temperatureTrend : 
windSpeed        : 6 mph
windDirection    : NNW
icon             : https://api.weather.gov/icons/land/night/sct?size=medium
shortForecast    : Partly Cloudy
detailedForecast : Partly cloudy, with a low around 23.

number           : 14
name             : Monday
startTime        : 11/22/2021 6:00:00 AM
endTime          : 11/22/2021 6:00:00 PM
isDaytime        : True
temperature      : 44
temperatureUnit  : F
temperatureTrend : 
windSpeed        : 3 to 7 mph
windDirection    : ENE
icon             : https://api.weather.gov/icons/land/day/sct?size=medium
shortForecast    : Mostly Sunny
detailedForecast : Mostly sunny, with a high near 44.

Hourly Forecast

To get the hourly forecast, you can use the ForecastHourly property to retrieve the proper URL.

$Result = Invoke-RestMethod 'https://api.weather.gov/points/43.5129009,-114.3315179'
(Invoke-RestMethod ($Result.Properties.ForecastHourly)).Properties.periods

The result looks like this.

number           : 1
name             : 
startTime        : 11/15/2021 6:00:00 PM
endTime          : 11/15/2021 7:00:00 PM
isDaytime        : False
temperature      : 47
temperatureUnit  : F
temperatureTrend : 
windSpeed        : 8 mph
windDirection    : SSE
icon             : https://api.weather.gov/icons/land/night/bkn?size=small
shortForecast    : Mostly Cloudy
detailedForecast : 

number           : 2
name             : 
startTime        : 11/15/2021 7:00:00 PM
endTime          : 11/15/2021 8:00:00 PM
isDaytime        : False
temperature      : 46
temperatureUnit  : F
temperatureTrend : 
windSpeed        : 9 mph
windDirection    : SW
icon             : https://api.weather.gov/icons/land/night/rain_showers,20?size=small
shortForecast    : Slight Chance Rain Showers
detailedForecast : 

number           : 3
name             : 
startTime        : 11/15/2021 8:00:00 PM
endTime          : 11/15/2021 9:00:00 PM
isDaytime        : False
temperature      : 45
temperatureUnit  : F
temperatureTrend : 
windSpeed        : 10 mph
windDirection    : W
icon             : https://api.weather.gov/icons/land/night/rain_showers,20?size=small
shortForecast    : Slight Chance Rain Showers
detailedForecast : 

number           : 4
name             : 
startTime        : 11/15/2021 9:00:00 PM
endTime          : 11/15/2021 10:00:00 PM
isDaytime        : False
temperature      : 43
temperatureUnit  : F
temperatureTrend : 
windSpeed        : 12 mph
windDirection    : W
icon             : https://api.weather.gov/icons/land/night/rain_showers,30?size=small
shortForecast    : Chance Rain Showers
detailedForecast : 

number           : 5
name             : 
startTime        : 11/15/2021 10:00:00 PM
endTime          : 11/15/2021 11:00:00 PM
isDaytime        : False
temperature      : 43
temperatureUnit  : F
temperatureTrend : 
windSpeed        : 14 mph
windDirection    : W
icon             : https://api.weather.gov/icons/land/night/rain_showers,60?size=small
shortForecast    : Rain Showers Likely
detailedForecast : 

wttr.in

Jamie Booth pointed out the amazing wttr.in project. You can simplify run the following command to view the weather.

Invoke-RestMethod https://wttr.in

The result is the weather you would expect in your console.