I thought it would be a fun project to create a PowerShell function which would get the Weather forecast for a specified location.

I started searching online for free Weather REST APIs which I could query for the forecast. I found api.weather.gov which has a completely free and open REST API with complete documentation API Web Service (weather.gov). Checking the documentation, the REST endpoint to query the forecast is https://api.weather.gov/gridpoints/{office}/{grid X},{grid Y}/forecast. To find the grid points, we can use the /points endpoint with latitude and longitude co-ordinates https://api.weather.gov/points/{latitude},{longitude} . I found a free web service which will provide latitude and longitude based on zip code https://api.promaptools.com/service/us/zip-lat-lng/get/?zip=$zipcode&key=17o8dysaCDrgv1c .

After fiddling around with Invoke-RestMethod to get the data I wanted, I came up with this function in PowerShell.

function Get-WeatherForecast {
param (
[parameter(mandatory=$true)]
[string]$zipcode
)

#region Weather Variables

#Get Lattitude and Longitude for your zip code
$getLatLong = Invoke-restmethod -uri "https://api.promaptools.com/service/us/zip-lat-lng/get/?zip=$zipcode&key=17o8dysaCDrgv1c" | select -ExpandProperty output

#Get weather forecast endpoint for your zipcode
$getPoints = Invoke-RestMethod -uri "https://api.weather.gov/points/$($getLatLong.latitude),$($getLatLong.longitude)"

#Get 7 day weather forecast
$weatherGet = Invoke-RestMethod -Uri $getPoints.properties.forecast

#Get todays date. Manipulate string to match JSON date object returned from api.weather.gov
$TodayManipulate = ((get-date).ToShortDateString() -replace '/','-') -split '-'
$today = "$($TodayManipulate[2] + '-' + $TodayManipulate[0] + '-' + $TodayManipulate[1])*"

# Do the same for tomorrows date
$TomorrowManipulate = (((get-date).AddDays(1)).ToShortDateString() -replace '/','-') -split '-'
$tomorrow = "$($TomorrowManipulate[2] + '-' + $TomorrowManipulate[0] + '-' + $TomorrowManipulate[1])*"

# Get today and tomorrows forecasted temperatures
$todaysTemp = Invoke-RestMethod -Uri 'https://api.weather.gov/gridpoints/LWX/102,88' | select -ExpandProperty properties | select -ExpandProperty temperature | select -ExpandProperty values | select -ExcludeProperty values | where validtime -like "$($today)" | sort value
$tomorrowsTemp = Invoke-RestMethod -Uri 'https://api.weather.gov/gridpoints/LWX/102,88' | select -ExpandProperty properties | select -ExpandProperty temperature | select -ExpandProperty values | select -ExcludeProperty values | where validtime -like "$($tomorrow)" | sort value

# Convert temps to farenheit
$TodaysTempF = $todaysTemp | select @{n="Temp";e={$_.value | foreach {[int]($_ * 1.8) + 32}}}
$tomorrowsTempF = $tomorrowsTemp | select @{n="Temp";e={$_.value | foreach {[int]($_ * 1.8) + 32}}}

# Get low and high temperatures, selecting by index of array
$LowTempToday = ($TodaysTempF.Temp)[0]
$LowTempTomorrow = ($tomorrowsTempF.Temp)[0]
$HighTempToday = ($TodaysTempF.Temp)[-1]
$HighTempTomorrow = ($tomorrowsTempF.Temp)[-1]

# Filter 7 day forecast for today and tomorrows forecast
$forecast = $weatherGet | select -ExpandProperty properties | select -ExpandProperty periods | where number -eq 1
$tomorrowsForecast = ($weatherGet | select -ExpandProperty properties | select -ExpandProperty periods | where startTime -like $tomorrow)[0]
#endregion


Write-Host "Forecast for $((Get-Date).ToShortDateString())" -foregroundcolor Green -backgroundcolor Black
Write-Host  "Todays Low Temperature: $($LowTempToday)"
Write-host "Todays High Temperature: $($HighTempToday)"

Write-Host "Detailed Forecast: `n$($forecast.detailedForecast)" -ForegroundColor Green -BackgroundColor Black

write-host "`nTomorrows Low Temperature: $($LowTempTomorrow)"
write-host "Tomorrows High Temperature: $($HighTempTomorrow)"

Write-Host "`nTomorrows Forecast:`n$($tomorrowsForecast.detailedForecast)" -foregroundcolor Green -BackgroundColor Black

}

All in all, this project took about an hour. I have since combined it with a scheduled task on one of my VM’s to send me a daily email with the forecast using the Microsoft Graph SendMail API. For a tutorial on sending mail using Graph, I recommend checking out the tech guy blog Send Mail with PowerShell and Microsoft Graph API - TechGuy