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.
I help organisations secure their cloud infrastructure and stay ahead of evolving cyber threats. Microsoft MVP and Certified Trainer, author of Mastering Azure Security, and founder of arnav.au — a platform for practical Cloud, Cybersecurity, DevOps and AI content.
Frequently Asked Questions
The Test-Connection cmdlet is used to ping multiple IP addresses and hostnames in PowerShell. It sends ICMP echo request packets to the specified targets and returns the results, allowing you to check the connectivity status of each target.
You can modify the $targets array in the script to include the IP addresses and hostnames you want to ping. Simply replace the existing values with your own addresses, such as $targets = @("192.168.1.1", "google.com", "your-server.com"), and the script will loop through and ping each one.
The -Count parameter specifies the number of echo requests to send to each target. In the sample script, it's set to 2, meaning two ping attempts will be made for each IP address or hostname to provide more reliable connectivity results.
The -ErrorAction SilentlyContinue parameter handles errors silently without stopping the script execution. This is helpful because if one ping fails, the script will continue to ping the remaining targets instead of terminating unexpectedly.
If you encounter an execution policy error, you can adjust your script execution policy using the Set-ExecutionPolicy cmdlet before running the PowerShell script. This allows the .ps1 file to execute properly on your system.