Simple Windows PowerShell & .BAT Script to Pings Hosts defined in a .txt file ( Windows)

We recently had a load of CCTV camera on various networks, we needed to determine which cameras where up and which where down. To get around this issue as the IP addresses belonging to different network ranges, rather than using my go to tool NMAP the below script was used, this would report back a simple “Ping OK” if the host was up or “Ping Fail” if the host was down

This script references IP addresses defined in the file c:\ping\yourtextfile.txt

ForEach ($targetComputer in (Get-Content C:\PING\YOURTEXTFILE.txt)) {
    if (Test-Connection -ComputerName $targetComputer -Count 1 -Quiet) {
        "$targetComputer - Ping OK"
    } else {
        "$targetComputer - Ping FAIL"

The results of the script will look like the below.

The .BAT method

If you prefer to not use powershell you can achieve the same result using the below code saved with a .bat extension.

In this example you need to put the “cctv.txt” file in the same directory that you are running the batch file from.

@Echo OFF

For /F "Usebackq Delims=" %%# in (
) do (
    Echo [+] Pinging: %%#

    Ping -n 1 "%%#" 1>nul && (
        Echo     [OK]) || (
        Echo     [FAILED])


Very Simple but very handy at times.

Leave a Reply

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