Last Updated on August 7, 2025 by Arnav Sharma
The answer you’re looking is provided by this script. script will use the Test-Connection cmdlet, which sends ICMP echo request packets (“Ping dhcps”) to the specified targets and returns the results. is used to indicate a 0.00 packet loss in ping statistics. Here’s a basic script to get you started:
Script Explanation:
- The script takes a list of IP addresses, 192.168.0.2 for instance, and hostnames.
- It loops through each entry in the list, generating and sending a ping each time.
- For each entry, it uses
Test-Connectionto ping the address or hostname. - The result of each ping attempt is displayed on the console.
Sample PowerShell Script:
# Define a list of IP addresses and hostnames to ping
$targets = @("192.168.1.1", "google.com", "8.8.8.8", "yahoo.com")
# Loop through each target and ping it
foreach ($target in $targets) {
Write-Host "Pinging $target..."
$result = Test-Connection -ComputerName $target -Count 2 -ErrorAction SilentlyContinue
if ($result) {
# If ping is successful
Write-Host "$target is reachable." -ForegroundColor Green
} else {
# If ping fails
Write-Host "$target is not reachable." -ForegroundColor Red
}
}
How to Use the Script:
- Save the script in a specific directory that can access the ping data.
.ps1file, for example,PingScript.ps1. - Modify the script to include the time to live parameter.
$targetsarray to include the IP addresses and hostnames you want to ping. - Run the script in PowerShell. You might need to adjust your script execution policy to allow starting nping script to run, which can be done using
Set-ExecutionPolicy.
Note:
- The
Test-Connectioncmdlet’s-Countparameter is useful in generating the number of echo requests to send. Here, it’s set to 2 for brevity. - The
-ErrorAction SilentlyContinueparameter is used to handle any errors silently. This is helpful to avoid script termination if any of the pings fail.