# Script to Ping a Website on Windows

*Ping* is a command-line utility that checks if an IP address or host is accessible over a network connection. To check the accessibility of a website, the *Ping* command sends data packets to the target address and waits for the response. A successful ping is registered if the data packets are returned without damage. IT admins can remotely troubleshoot network connectivity issues on their endpoint devices using the *Ping* command. This document covers the [custom script](https://www.hexnode.com/mobile-device-management/help/executing-custom-scripts-for-windows/) to remotely ping a website on a Windows device using Hexnode UEM.

 Note: 
**Supported Versions**:
The scripts given below will be supported on the following versions:

- Windows 10 v1607+ (Pro, Enterprise, Education)
- Windows 11 (Pro, Enterprise, Education)

 

 Disclaimer:The sample scripts provided below are adapted from third-party open-source sites.

 

Batch Script 
-------------

Sample batch script to ping a website

@echo off ping -n 1 %1 | find "TTL=" > nul if errorlevel 1 ( echo The site is not reachable ) else ( echo The site could be pinged )

   1

2

3

4

5

6

7

  @echo off

ping -n 1 %1 | find "TTL=" > nul

if errorlevel 1 (

 echo The site is not reachable

) else (

 echo The site could be pinged

)

   

 

  

Pass the website to be pinged as the argument.

For example: `https://www.hexnode.com`

- `echo off` – To prevent displaying all commands in a batch file
- `Ping` – This command initializes the website ping request.
- `–n` – This flag is a count of the number of echo requests to send.
- `%1` – Represents the first argument passed by the admin, in this case, website URL or IP address.
- `TTL` – Stands for Time to Live. Only if the packet’s time to live is greater than 0, the `errorlevel` is checked.
- `errorlevel` – This command returns an integer. Any value other than 1 is considered a successful website ping, and the corresponding message is displayed.

PowerShell Script 
------------------

Sample powershell script to ping a website

Param($Hostname) Process { "Computer to ping: $Hostname" $ping = get-wmiobject -Query "select \* from win32\_pingstatus where Address='$Hostname'" if ($ping.statuscode -eq 0) { "Website is accessible and responded in: {0}ms" -f $ping.responsetime } else {"Website did not respond"} } 

   1

2

3

4

5

6

7

8

9

10

11

12

  Param($Hostname)

Process

{

"Computer to ping: $Hostname"

$ping = get-wmiobject -Query "select \* from win32\_pingstatus where Address='$Hostname'"

if ($ping.statuscode -eq 0)

{

"Website is accessible and responded in: {0}ms" -f $ping.responsetime

}

else

{"Website did not respond"}

} 

   

 

  

Pass the website to be pinged as the argument.

For example: `https://www.hexnode.com `

- `$Hostname` – Contains the website passed as the argument.
- `$ping` – Initializes the ping request for the target website.
- `get-wmiobject` – Gets the instances of Windows Management Instrumentation (WMI) classes or information about the available classes.
- `win32_pingstatus` – Gives the values returned by the `$ping` command.
- `$ping.Statuscode` – This returns an integer value. If the value is equal to 0, a success message is displayed. Any value other than 0 is considered an error.
- `$ping.responsetime` – Displays the time taken by the website to respond.

 Notes:- It is recommended to manually validate the script execution on a system before executing the action in bulk.
- Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.