Ping a List of Computers

| February 28th, 2018

Have you ever had a list of IP addresses that you need to ping, and check for DNS entry? Have you ever had to do it manually, one at a time? I did, wayyyyyy before I learned how to automate the mundane.

Now, the following script is not as pretty as it could be, but it serves me fine because I just need it to get the data, and I do the rest of the formatting in Excel.

#Create your output file with headers.
#(it will do this each time you run the script, to reset the file.)
$OUTPUT = “IP| STATUS| HOSTNAME” | Out-File .\pingList-Report.csv

#Import your list of IP addresses. They should be in a file called ipaddresses.csv, single column with a header of “ipaddress”.
$TOPING = (import-csv .\ipaddresses.csv).ipaddress

#Loop through the IP Addresses from the ipaddresses.csv file
foreach ($ip in $TOPING){

#First, resolve the hostname by performing a DNS lookup.
$HOSTNAME = (resolve-dnsname $ip -ErrorAction SilentlyContinue).namehost
#Ping the IP using the “test-connection” cmdlet, and if it responds (if the condition is true) set the status variable to “ONLINE”
if (Test-Connection $ip -Count 1 -Quiet) {
else {
#If it doesn’t respond (if the condition is anything but true) set the status variable to “OFFLINE”
#Output the results from this IP to the file, and move on to the next IP.
$OUTPUT = “$ip| $PINGSTAT| $HOSTNAME” | Out-File .\pingList-Report.csv -Append

That’s the whole script. The input file (ipaddresses.csv) should be formatted as such:


The Output file will look similar to this:


Once you have this CSV, you can import ==> From Text File in Excel, and set the delimter as the pipe “|” character.

You may be wondering why I use the pipe instead of actually using a CSV. Well, I pull a lot of data from Active Directory and Exchange using powershell, and unfortunately its still not easy to separate fields such as “DisplayName”, which is typically “Lastname, Firstname”. The comma between the two names tends to be read as two separate fields when you import into Excel. By using Out-File and delimiting using pipe or “|” it allows me to use a character that is rarely used in any field in either AD or Exchange, and ensures that there is no confusion in fields that contain a comma as part of the data.

Thanks for reading, and feel free to post comments below. I’d love to hear if this script worked for you, and/or any enhancements you may have made to it!


Leave a Reply

%d bloggers like this: