Windows 10

How to Check Internet Connection using Batch File

In this tutorial, you will learn how to use a batch file to check if your computer is connected to the internet. A batch file contains a sequence of DOS (Disk Operating System) commands that execute automatically.
 

✅ Check Internet Connection Using ping Command

The following batch script uses the ping command to test internet connectivity by pinging www.google.com. If the ping is successful, it displays a message indicating that the internet is working. Otherwise, it shows a connection error message.
 
📄 Batch File Example:

@echo off
Ping www.google.com -n 1 -w 1000 > nul
if errorlevel 1 (
    echo You are not connected to the internet
) else (
    echo You are connected to the internet
)

📤 Output:

You are connected to the internet

or (if disconnected):

You are not connected to the internet

 

🧠 Explanation of the Commands:
Command Description
Ping www.google.com Sends a ping request to Google’s server.
-n 1 Sends only one ping packet.
-w 1000 Sets a timeout of 1000 milliseconds (1 second).
> nul Hides the output of the ping command from the console.
if errorlevel 1 Checks the exit code. If 1 or higher, the ping failed.
echo Displays a message in the console.

 

📌 Use Cases:
  • Quickly check internet status before running a script.
  • Automate diagnostics in IT environments.
  • Include in login scripts for offline/online detection.
💡 Bonus Tip:

To log the result to a file, you can modify the script like this:

@echo off
Ping www.google.com -n 1 -w 1000 > nul
if errorlevel 1 (
    echo %date% %time% - No internet >> internet_status.log
) else (
    echo %date% %time% - Connected >> internet_status.log
)

This will append the connection status along with the date and time to a log file named internet_status.log.
 

Leave a Reply

Your email address will not be published. Required fields are marked *