Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Baby, It’s Cold Outside

Posted on February 16, 2015

I don't know about your neck of the woods, but it is downright Arctic here. So I thought I'd polish up my PowerShell function to get weather data.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!
#requires -version 3.0

Function Get-MyWeather {
<#
.Synopsis
Get the weather for a US zip code
.Description
This command will query the Yahoo weather service and return weather conditions for a US zip code.
.Example
PS C:\> get-myweather 98133

Location    : Seattle, WA
Date        : 2/16/2015 4:53:00 AM
Temperature : 45
Low         : 38
High        : 60
Conditions  : Mostly Cloudy

.Notes
Learn more about PowerShell:
Essential PowerShell Learning Resources
**************************************************************** * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. * **************************************************************** .Link Invoke-RestMethod #> Param( [Parameter(Position=0,HelpMessage="Enter a United States zip code")] [ValidateNotNullorEmpty()] [string]$Zip='00000' ) #construct the URI $uri = "http://xml.weather.yahoo.com/forecastrss?p=$Zip" Try { #gets XML data $data = Invoke-RestMethod -Uri $uri -ErrorAction Stop write-verbose ($data | out-string) if ($data.title -ne 'city not found') { #get title string and parse out location and time using #named regular expression captures $rx = "(?<location>\S+,\s\S+) at (?<time>\d{1,2}:\d{2}\s(a|p)m)" $data.title -match $rx | out-Null Write-Verbose ($matches | out-string) $location = $matches.location $dt = $matches.time -as [datetime] #write a custom object to the pipeline [pscustomobject][ordered]@{ Location = $location Date = $dt Temperature = $data.condition.temp -as [int] Low = $data.forecast[0].low -as [int] High = $data.forecast[0].high -as [int] Conditions = $data.condition.text } #close hash table } else { Write-Warning "No city found for zip code $zip" } } #Try Catch { #throw the exception Throw $_ } } #end function

This function uses the Yahoo weather RSS feed for US locations. The function uses the Invoke-RestMethod to get an entry for a given zip code. I set my zip code as the default and suggest you do the same. You will need to modify the default value in the code above. Invoke-RestMethod gives me an XML document so it isn't too difficult to pull out the values I want and construct a custom object. I even use some regular expression named captures to break out the location and time. So hopefully there are some good learning examples here.

Anyway, quite frigid here, and this is already 5 degrees warmer than when I got up.

weather

Hope you are staying warm.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

5 thoughts on “Baby, It’s Cold Outside”

  1. Paul Cassidy aka qawarrior says:
    February 16, 2015 at 5:16 pm

    Thanks for the fun code sample, Jeff. Sorry to hear that your freezing your wiggly parts off. I ashamed to say that we here in the Nor-Cal Posh area are suffering through days with highs in the mid 70’s, that’s right it was 75 here yesterday 2/15/15. Thanks Again for the fun posh sample and for god’s sake put a jacket on. Gotta go water skiing (joke). See Ya In Cyber Space, and keep the PShell open.

    1. Jeffery Hicks says:
      February 16, 2015 at 9:16 pm

      Fortunately I work at home so I don’t have to venture out.

  2. hal (@grundil) says:
    February 17, 2015 at 4:45 pm

    Love the script Jeff, however you should be aware that the P= option has been depreciated by yahoo and will be phased out in the next release. I’m in Canada myself so obviously your script won’t work for me due to the difference in zip/postal codes so I’m trying to find a way to be able to query for the woeid, of course yahoo doesn’t exactly have the greatest documentation on how to find said beast for a given location without loading up the webpage and copying it from there.

    1. Jeffery Hicks says:
      February 17, 2015 at 4:55 pm

      Look at the functions I posted today on using the WOEID.

      1. hal (@grundil) says:
        February 17, 2015 at 6:59 pm

        Well damn, didn’t see that 🙂 Thanks Jeff!

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d