Yesterday, we reviewed a nice and useful service, wttr.in, which allows the user to fetch the weather forecast in Linux terminal. Today, we'll learn how to do the same in Windows PowerShell.
Advertisеment
We will use an open source web service wttr.in to fetch the weather forecast. Wttr.in can be used not only to check the weather, but also for some other purposes. For example, you can see the current Moon phase.
PowerShell is an advanced form of command prompt. It is extended with a huge set of ready-to-use cmdlets and comes with the ability to use .NET framework/C# in various scenarios. See All ways to open PowerShell in Windows 10.
In PowerShell, there is a special alias "curl" for the built-in cmdlet Invoke-RestMethod, which can retrieve the URL contents from the PowerShell console. This will allow us to use commands I've provided in the Linux article almost without modification.
To get the weather forecast in PowerShell, you can use the following commands.
To get the current weather in PowerShell, type or copy-paste the following command:
(curl http://wttr.in/?Q0 -UserAgent "curl" ).Content
You can specify the desired location as follows:
(curl http://wttr.in/NewYork -UserAgent "curl" ).Content
The output will be as follows:
You can specify the country where you live when required. The syntax is as follows:
(curl http://wttr.in/"Madrid,Spain" -UserAgent "curl" ).Content
Double quotes are important to ensure that the location will be passed to the service, otherwise you will get an error in PowerShell.
The service supports a number of options. Open the following page to learn about them:
http://wttr.in/:help
Alternatively, you can use this command in your terminal:
(curl http://wttr.in/:help -UserAgent "curl" ).Content
Here are some useful options.
(curl wttr.in/New-York?n -UserAgent "curl" ).Content
This will display the short version of the forecast which includes only Noon and Night.
(curl wttr.in/New-York?0 -UserAgent "curl" ).Content
This will show only the current weather in the specified location.
It is worth mentioning that the wttr.in service can show the forecast right in your web browser. Point your browser to the same location you use in PowerShell. See the following screenshot:
If you add ".png" to the location, the service will return a PNG image. You can embed it in your web page.
For example, open this link: http://wttr.in/New-York.png
When in PNG mode, you can pass parameters as follows:
wttr.in/Location_parameters.png
For example:
wttr.in/New-York_tq0.png
The service is localized into several languages.
To change the forecast language, you can use the following syntax:
(curl wttr.in/Berlin?lang=de -UserAgent "curl" ).Content (curl wttr.in/Moscow?lang=ru -UserAgent "curl" ).Content
Alternatively, you can use subdomains as follows:
(curl de.wttr.in/Berlin -UserAgent "curl" ).Content (curl ru.wttr.in/Moscow -UserAgent "curl" ).Content
Supported languages are:
az bg ca cs cy da de el eo es fi fr hi hr hu is it ja ko mk ml nl nn pt pl ro ru sk sl sr sr-lat sv tr uk uz vi zh et hy jv ka kk ky lt lv sw th zu bs be
Wttr.in can be used to see the current Moon phase. Execute the following command:
(curl wttr.in/Moon -UserAgent "curl" ).Content
Note: All screenshots above were made in Windows 10 Creators Update. In previous Windows versions, there can be an issue with ANSI sequences in PowerShell. If you are running Windows 7 or Windows 8, please refer to the following page to get a workaround: How to enable wttr.in in a PowerShell console.
That's it.
Support us
Winaero greatly relies on your support. You can help the site keep bringing you interesting and useful content and software by using these options:
If you like this article, please share it using the buttons below. It won't take a lot from you, but it will help us grow. Thanks for your support!
Advertisеment
Excellent again! More useful now as I am on Windows. :) Thanks Sergey!
You are welcome. :-)
Thanks for another informative article, I received an error and realized I had to add -UseBasicParsing to the command which I think is because I uninstalled IE11 if anyone else gets an error the following worked for me.
(curl wttr.in/New-York -UseBasicParsing -UserAgent “curl” ).Content
Thats pretty cool.
How do i get workaround for win7? https://gist.github.com/chubin/22a239783c67456eb0de
Use the sample script provided by the author. Save as a PS1 file and run.
it gives me error
http://i.imgur.com/cHJw179.png
Can you show me the whole script?
https://www.anonfiles.cc/file/6edb59b2a29135fe1c7cac5a389fb188
It might slow download speed
sha256sum: 46371BD8C52150EAAACB9F4F30218AC74AA6044B6B458934B355357DB5263CB3
it doesn’t work in windows 7
Even after the mentioned workaround?
how do I workaround on windows 7 with powershell script?
save it as a ps1 file and run.
This seems to work well on PS 5.1 in Windows 11.
However when I try to run it in PS 7.2.1, I get the following error:
PS C:\Users\starf> (curl http://wttr.in/NewYork -UserAgent “curl” ).Content
Enter proxy password for user ‘serAgent’:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 8738 100 8738 0 0 8738 0 0:00:01 –:–:– 0:00:01 21522
0 0 0 0 0 0 0 0 –:–:– 0:00:01 –:–:– 0curl: (6) Could not resolve host: curl
Any idea what I am doing wrong?
Try with
curl http://wttr.in/NewYork