Ping List of Computers in PowerShell

Ping List of Computers in PowerShell

Different Ways to Ping a List of Computers in PowerShell

There are multiple ways that we can use to ping a list of computers in PowerShell. Now, what does ping means? Pinging a computer means determining if the networked computer is available and responsive. If the networked computer (we can also call it a target machine) is available and responds, it’ll send a reply to the sender who initiated the ping request. This reply from the target machine indicates that it is online and running.

We also ping the computer or list of computers to check network issues and verify the network connectivity. For instance, pinging a computer helps us understand whether the issue is with the target computer itself or is related to network connectivity. To ping a computer, we can use the following ways:

  • Using Test-Connect Cmdlet
  • Using WMI and Get-WmiObject Cmdlet
  • Using the System.Net.NetworkInformation.Ping Class

To learn how to ping a list of computers, we have saved the IPs of all computers in a text file (computersList.txt) containing the following data.

Please note that you can replace above IP addresses with Computer names as well.

Now, we know what ping is and why we use it. We also have a list of computers to ping, so let’s move towards the various methods to ping these computers.

Using Test-Connect Cmdlet

To ping a list of computers’ IP in PowerShell:

  • Use the Get-Content cmdlet to retrieve a list of computers from computerList.txt and save it in a variable.
  • Use the foreach loop to iterate over each computer in a computer’s list that we created in the previous step.
  • In each iteration of the foreach loop:
    • Use the Test-Connect cmdlet to send a ping request and save the reply in a variable.
    • Use if-else to print if the current computer is up/down based on the specified condition.

First, we used the Get-Content cmdlet to read the content of the computersList.txt file and saved it in the $computers variable. The computersList.txt lives in the current directory; you can put this file wherever you want on your local machine. The computersList.txt contained the IPs of the computers, but computer names can also be used here.

Next, we used a foreach loop to iterate over the $computers array. In each iteration, we used Test-Connection with -Count and -Quiet parameters to ping the current computer and saved the reply in the $pingComputer variable. Here, the Test-Connection cmdlet sent the Internet Control Message Protocol (ICMP) echo request packet (pings) to one or multiple computers.

We can use the Test-Connection cmdlet to check whether a specific computer is available and responsive across an IP network. We can also use its parameters to mention sending and receiving machines, to set a time-out and pings count, to run the command as the background job, and to authenticate or configure a specific connection. You can find more about it here.

The Test-Connection cmdlet returns TestConnectionCommand+PingStatus as an object we can use in PowerShell. However, in the above example, we are not getting the TestConnectionCommand+PingStatus as an object; why? It is because we used the -Quiet parameter, which returned a Boolean value, true if the target machine is up; otherwise, false. In addition, the -Count parameter was used to specify that ping only one computer at a time.

Finally, we used the if-else block and checked if the $pingComputer is true. If it is, the if block will be executed; otherwise, the else block will run. Finally, to display a message on the PowerShell console, we used the Write-Host cmdlet, which is used to write the customized output to the host (host means PowerShell console).

Similarly, can ping using computer’s names instead of their IPs.

Using WMI and Get-WmiObject Cmdlet

To ping a list of computers’ IPs in PowerShell:

  • Use the Get-Content cmdlet to retrieve a list of computers from computerList.txt and save it in a variable.
  • Use the foreach loop to iterate over each computer in a computer’s list that we created in the previous step.
  • In each iteration of the foreach loop:
    • Use the Win32_PingStatus cmdlet to send a ping request and store the extracted Status in a variable.
    • Use if-else with the -eq operator to print if the current computer is up/down based on the given condition.
    • Enclose the code within the foreach with a try-catch block to avoid breaking the script and handle an exception if it occurs.

Like the previous example, we used the Get-Content cmdlet to access the data of computersList.txt and saved it in the $computers array. Then, we used the foreach statement to loop over the $computers array.

In each iteration, we used Get-WmiObject cmdlet to query the Windows Management Instrumentation (WMI) class named Win32_PingStatus and got the ping request’s Status for the current target computer ($computer). First, we used the -Filter parameter to write the target computer. Next, we used the Select-Object cmdlet to extract the StatusCode property from the received WMI object; the final result was stored in the $pingComputer variable.

Then, we used the if statement with the -eq comparison operator to determine if the value of $pingComptuer is equal to 0 or not. If it is, display that the current computer is up; otherwise, it is down. You may have noticed that we wrapped the foreach loop’s body with a try-catch block to handle the exception if it occurs.

Now, let’s ping various computer using their names, also called host names.

Using System.Net.NetworkInformation.Ping Class

To ping a list of IPs of different computers in PowerShell:

  • Use the Get-Content cmdlet to access a list of computers from computerList.txt and save it in a variable.
  • Use the foreach loop to iterate over each computer in a computer’s list that we created in the previous step.
  • In each iteration of foreach:
    • Use the New-Object cmdlet to create an object of the System.Net.NetworkInformation.Ping class and save it in a variable.
    • Use the .Send() method to send a ping request to the current target computer and save the response in a variable.
    • Use if-else to print if the current computer is up/down based on the specified condition.
    • Enclose the code within the foreach with try-catch to handle an exception if it occurs.

This code is similar to the previous one except few changes. First, we used the New-Object cmdlet to create an object of the System.Net.NetworkInformation.Ping class and saved it in the $pingComputer variable. Next, we created an object of this class to access its .Send() method to send a ping request by taking a $computer as an argument.

The .Send() method sends an ICMP echo message to the given computer and gets a corresponding ICMP echo response from that computer; we saved this response in the $response variable. Finally, we used the if statement with the -eq operator to determine if the $response.Status is equal to Success. If it is, then show that the target machine is up and running; otherwise, it is down. Finally, we used the try-catch block to avoid breaking the script and handle the exceptions if any occurred.

Let’s use the same code to ping a list of computers using their names.

That’s all about how to ping List of Computers in PowerShell.

Was this post helpful?

Leave a Reply

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