I came across some fun stuff on the internet, so I wrote a quick wrapper to make a Powershell weather forecasting Function for this blog post.

The idea is to get the weather report in Powershell console using this  cURL implementation converted into Powershell function below

PowerShell Weather Forecasting Function:


<#
.SYNOPSIS
Get Weather of a City
.DESCRIPTION
Fetches Weather report of a City from website – http://wttr.in/ courtsey Igor Chubin [Twitter- @igor_chubin]
.PARAMETER City
Name of City
.PARAMETER Tomorrow
Switch to include tomorrow's Weather report
.PARAMETER DayAfterTomorrow
Switch to include Day after tomorrow's Weather report
.EXAMPLE
Get-Weather "Los Angles" -Tomorrow -DayAfterTomorrow
.EXAMPLE
'london', 'delhi', 'beijing' | Get-Weather
.NOTES
Blog – RidiCurious.com
#>
Function Get-Weather {
[Alias('Wttr')]
[Cmdletbinding()]
Param(
[Parameter(
Mandatory = $true,
HelpMessage = 'Enter name of the City to get weather report',
ValueFromPipeline = $true,
Position = 0
)]
[ValidateNotNullOrEmpty()]
[string[]] $City,
[switch] $Tomorrow,
[switch] $DayAfterTomorrow
)
Process
{
Foreach($Item in $City){
try {
# Check Operating System Version
If((Get-WmiObject win32_operatingsystem).caption -like "*Windows 10*") {
$Weather = $(Invoke-WebRequest "http://wttr.in/$City" -UserAgent curl).content -split "`n"
}
else {
$Weather = (Invoke-WebRequest "http://wttr.in/$City").ParsedHtml.body.outerText -split "`n"
}
If($Weather)
{
$Weather[0..16]
If($Tomorrow){ $Weather[17..26] }
If($DayAfterTomorrow){ $Weather[27..36] }
}
}
catch {
$_.exception.Message
}
}
}
}
view raw

Get-Weather.ps1

hosted with ❤ by GitHub

Just pass a name of the City as a parameter to the Function to get the weather report of the day.

PowerShell Weather

You can pass values through the pipeline as well.

PowerShell Weather forecast

Use ‘-Tomorrow‘ and ‘-DayAfterTomorrow‘ to check the weather of later than today.

PowerShell Weather

To add this script to your Powershell profile run the below piece of code and you are good to go


Add-Content $profile -Value (iwr https://goo.gl/UexVKY); . $Profile

If you like this article, you may want to take a look at my Web and Native API Blog series for similar and other interesting implementations in PowerShell.

Don’t forget to show off your Powershell-Weather skills to your colleagues, Cheers! 😉

Subscribe to our mailing list

* indicates required