Skip to main content

How to Get the Weather Forecast in Batch Script (via PowerShell)

While batch scripting is great for local system tasks, it has no native, built-in ability to connect to the internet to fetch live data like a weather forecast. This is a task that requires a modern web request, something cmd.exe was never designed to do.

However, a batch script can act as a perfect launcher for a more powerful tool that is built into Windows: PowerShell. This guide will teach you how to use a simple PowerShell one-liner from within your batch script to connect to a free, public weather service and display the current weather for any location.

The Challenge: Batch Can't Access the Web

The cmd.exe interpreter has no commands like HTTP_GET or DOWNLOAD_TEXT. To get external data from a web service, a batch script must delegate the task to a tool that can speak the language of the modern web (HTTP/HTTPS).

The Solution: Using a Web-Based Weather Service (wttr.in)

For this example, we will use wttr.in, a fantastic, free, and open-source weather forecast service designed for the command line. It's perfect for scripting because:

  • It requires no API keys or registration.
  • It can return the weather as simple, pre-formatted plain text.

By making a web request to a URL like http://wttr.in/London, we can get the weather. We can also add formatting options to get a very simple, one-line response.

The best and most reliable way to make this web request is with a PowerShell one-liner. It uses the Invoke-RestMethod cmdlet, which is designed for this purpose.

Syntax: powershell -Command "(Invoke-RestMethod -Uri 'wttr.in/<Location>?format=3').Trim()"

  • Invoke-RestMethod: The PowerShell cmdlet to get structured data from a web service.
  • wttr.in/<Location>: The URL for the weather service.
  • ?format=3: A special parameter for wttr.in that requests a very simple, one-line format (e.g., London: ☀️ +15°C).
  • .Trim(): A string method to clean up any extra whitespace from the result.

Basic Example: Getting the Weather for a Specific City

This script uses the PowerShell one-liner to get the weather for Paris and display it.

@ECHO OFF
SET "Location=Paris"
SET "WeatherReport="

ECHO --- Getting the Current Weather for %Location% ---
ECHO.

FOR /F "delims=" %%W IN (
'powershell -Command "(Invoke-RestMethod -Uri 'wttr.in/%Location%?format=3').Trim()"'
) DO (
SET "WeatherReport=%%W"
)

IF DEFINED WeatherReport (
ECHO %WeatherReport%
) ELSE (
ECHO [FAILURE] Could not retrieve the weather forecast.
)

Output:

--- Getting the Current Weather for Paris ---

Paris: ⛅️ +18°C

How the PowerShell method works:

  1. powershell -Command "...": The batch script executes the PowerShell engine without opening a new window.
  2. Invoke-RestMethod -Uri '...': PowerShell makes an HTTP GET request to the wttr.in server, asking for the page wttr.in/Paris?format=3.
  3. The wttr.in server looks up the weather for Paris and sends back a plain text response containing the formatted string.
  4. PowerShell writes this string to its standard output.
  5. The FOR /F loop in the batch script captures this output and stores it in the WeatherReport variable.

Common Pitfalls and How to Solve Them

Problem: No Internet Connection

If your computer cannot connect to the internet, or if wttr.in is temporarily down, the PowerShell command will fail.

Solution: The FOR /F loop will not execute, and your variable (WeatherReport) will remain undefined. The IF DEFINED check in the example script is the correct way to handle this. It allows you to provide a clean error message to the user.

Problem: The Location Contains Spaces

If your location is "New York" or "San Francisco", you cannot just put it directly into the URL. The space must be handled correctly.

Solution: The wttr.in service is clever and often handles spaces well, but the correct way to do this is to URL-encode the location. We can add this logic to our PowerShell command.

powershell -Command "$loc = [System.Net.WebUtility]::UrlEncode('%Location%'); (Invoke-RestMethod -Uri 'wttr.in/$loc?format=3').Trim()"

This is a more robust command that will correctly handle any location name with spaces or special characters.

Practical Example: An Interactive Weather Forecast Script

This script prompts the user for a location and then uses the robust, URL-encoded PowerShell method to get the current weather.

@ECHO OFF
SETLOCAL

:Prompt
ECHO.
SET "UserLocation="
SET /P "UserLocation=Enter a city to get the weather for (or type 'exit'): "

IF /I "%UserLocation%"=="exit" GOTO :EOF

SET "Weather="
REM --- Use the robust PowerShell command that handles spaces ---
FOR /F "delims=" %%W IN (
'powershell -NoProfile -Command "$loc = [System.Net.WebUtility]::UrlEncode('%UserLocation%'); (Invoke-RestMethod -Uri 'wttr.in/$loc?format=3').Trim()"'
) DO (
SET "Weather=%%W"
)

ECHO.
IF DEFINED Weather (
ECHO Current Weather: %Weather%
) ELSE (
ECHO [FAILURE] Could not retrieve weather. Check your connection or the city name.
)
ECHO ----------------------------------------------------
GOTO :Prompt

ENDLOCAL

Conclusion

While batch scripting has no native ability to access web services, it serves as an excellent launcher for PowerShell, which is a first-class tool for this purpose.

Key takeaways:

  • Do not attempt to get web data with pure batch. It is not possible.
  • The PowerShell Invoke-RestMethod cmdlet is the modern, recommended, and reliable solution.
  • Use a free, no-key-required service like wttr.in for simple and easy access to weather data.
  • Use a FOR /F loop to capture the PowerShell output into a batch variable.