Skip to main content

How to Check if a URL is Online in Batch Script

A common requirement for monitoring and diagnostic scripts is to check if a website or web service is online and accessible. This means more than just being able to "ping" the server; it means confirming that the web service itself is responding correctly. Windows Batch has no native command to make an HTTP web request, so this task requires calling a more powerful, built-in tool.

This guide will teach you the different methods for checking a URL's status. We will cover the simple (but incomplete) ping method, the modern curl command (available in Windows 10/11), and the most powerful and compatible method for all modern systems: using a PowerShell one-liner. For any reliable script, PowerShell is the recommended approach.

The Challenge: "Online" Means More Than a Ping

It is critical to understand the difference between a server being "pingable" and a website being "online."

  • Ping (ICMP): A ping request just asks the server, "Are you there?" The server can be running perfectly but have its web service (like IIS or Apache) crashed.
  • Web Request (HTTP): A true web request asks the web service, "Can you serve me this page?" It confirms that both the server and the web application are working. An HTTP status code of 200 OK is the universal sign of success.

A simple ping is not a reliable way to check if a website is online.

Method 1: The ping Command (Basic, but Incomplete)

This method only checks if the server is reachable on the network. It does not check the web service.

Syntax: ping -n 1 <hostname>

  • -n 1: Sends only one packet instead of the default four.
  • If the command succeeds, %ERRORLEVEL% is 0. If it fails, it's 1.

Method 2: The curl Command (Modern and Good)

The curl command is a powerful, modern utility for transferring data with URLs. It is included by default in Windows 10 and 11. It can make a true HTTP request.

Syntax: curl -s --head --fail "<URL>"

  • -s: Silent. Hides the progress meter.
  • --head: Only requests the HTTP headers, not the full page content. This is much faster.
  • --fail: This is the crucial switch. It makes curl return a non-zero exit code if the HTTP status is an error (like 404 Not Found or 500 Server Error).

For maximum compatibility (Windows 7 and newer) and power, a PowerShell one-liner is the best solution. It makes a true web request and can check the status code directly.

Syntax: powershell -Command "try { $resp = Invoke-WebRequest -Uri '<URL>' -UseBasicParsing; if ($resp.StatusCode -eq 200) { exit 0 } else { exit 1 } } catch { exit 1 }"

  • Invoke-WebRequest: The PowerShell cmdlet to make a web request.
  • -UseBasicParsing: A critical switch that prevents PowerShell from using the slow Internet Explorer engine, making it fast and reliable for scripts.
  • if ($resp.StatusCode -eq 200): This checks if the HTTP status is exactly 200 OK.
  • try { ... } catch { ... }: This handles connection errors (e.g., the server is offline) and ensures the script exits with an error code.
  • exit 0 / exit 1: These set the %ERRORLEVEL% for the batch script to read.

Basic Example: Checking a Website's Status

This script uses the recommended PowerShell method to check Google's status.

@ECHO OFF
SET "URL_TO_CHECK=https://www.google.com"
ECHO --- Checking status of %URL_TO_CHECK% ---

powershell -Command "try { $resp = Invoke-WebRequest -Uri '%URL_TO_CHECK%' -UseBasicParsing; if ($resp.StatusCode -eq 200) { exit 0 } else { exit 1 } } catch { exit 1 }"

IF %ERRORLEVEL% EQU 0 (
ECHO [SUCCESS] The URL appears to be online.
) ELSE (
ECHO [FAILURE] The URL is offline or returned an error.
)

How to Use the Result in a Script (%ERRORLEVEL%)

All three methods communicate their result via the %ERRORLEVEL% variable, making them easy to use in an IF statement.

  • ping: 0 means the host responded. 1 means it didn't.
  • curl --fail: 0 means the HTTP status was in the 2xx success range. Non-zero means it was an error (4xx or 5xx).
  • PowerShell: Our script is explicitly designed to exit 0 on success (status 200) and exit 1 on any failure.

Common Pitfalls and How to Solve Them

  • Firewalls: The most common issue. A firewall might be blocking your request. ping uses ICMP, which is often blocked. Web requests use TCP ports 80 (HTTP) and 443 (HTTPS). Ensure your network allows this outbound traffic.
  • curl is not available: If you are on an older system (Windows 7/8), curl.exe will not be present. Solution: The PowerShell method is more compatible and should be used instead.
  • SSL/TLS Certificate Errors: curl and PowerShell will fail if a website has an invalid HTTPS certificate. For testing, you can tell them to ignore this (though it's a security risk).
    • curl -k: The -k switch allows insecure connections.
    • PowerShell: Requires more complex code to ignore certificate errors.

Practical Example: A Website Monitoring Script

This script takes a list of websites and checks each one, providing a clear "UP" or "DOWN" status. It uses the robust PowerShell method.

@ECHO OFF
SETLOCAL
TITLE Website Status Monitor

SET "WebsiteList=https://www.google.com https://www.github.com https://a-fake-website-that-fails.com"

ECHO --- Website Health Check ---
ECHO Started at: %TIME%
ECHO.

FOR %%U IN (%WebsiteList%) DO (
ECHO Checking %%U ...

powershell -NoProfile -ExecutionPolicy Bypass -Command "try { $resp = Invoke-WebRequest -Uri '%%U' -UseBasicParsing -TimeoutSec 10; if ($resp.StatusCode -eq 200) { exit 0 } else { exit 1 } } catch { exit 1 }"

IF %ERRORLEVEL% EQU 0 (
ECHO [UP] - %%U is online.
) ELSE (
ECHO [DOWN] - %%U is offline or returned an error.
)
ECHO.
)

ECHO --- Check complete ---
ENDLOCAL

Conclusion

While ping can check for basic server connectivity, it is not a reliable way to see if a website is online.

  • The curl --fail command is a good, modern tool for performing a true HTTP check, but it is only available on Windows 10/11 and newer.
  • The PowerShell Invoke-WebRequest method is the overwhelmingly recommended best practice. It is the most powerful, flexible, and compatible method for all modern Windows systems, and it provides the most accurate results.