Updated March 13, 2023
Introduction to PowerShell Ping
The PowerShell ping command is also known as Test-Connection in the PowerShell cmdlet, sends the Internet Control Protocol Message (ICMP) echo packets or the ping to the one or more remote nodes, and in return, it receives the echo packets to determine the connectivity of the remote servers and also by using the various parameters to run the command as the background job, set number of times to send request, to set the timeout and configure the connection and authentication, and even this command can trace the route of the destination node.
Syntax of PowerShell Ping
Given below is the syntax mentioned:
Using Test-Connection:
Test-Connection
[-TargetName] <string[]>
[-Ping]
[-IPv4]
[-IPv6]
[-ResolveDestination]
[-Source <string>]
[-MaxHops <int>]
[-Count <int>]
[-Delay <int>]
[-BufferSize <int>]
[-DontFragment]
[-TimeoutSeconds <int>]
[-Quiet]
[<CommonParameters>]
Test-Connection
[-TargetName] <string[]>
-Repeat
[-Ping]
[-IPv4]
[-IPv6]
[-ResolveDestination]
[-Source <string>]
[-MaxHops <int>]
[-Delay <int>]
[-BufferSize <int>]
[-DontFragment]
[-TimeoutSeconds <int>]
[-Quiet]
[<CommonParameters>]
Test-Connection
[-TargetName] <string[]>
-MtuSize
[-IPv4]
[-IPv6]
[-ResolveDestination]
[-TimeoutSeconds <int>]
[-Quiet]
[<CommonParameters>]
Test-Connection
[-TargetName] <string[]>
-Traceroute
[-IPv4]
[-IPv6]
[-ResolveDestination]
[-Source <string>]
[-MaxHops <int>]
[-TimeoutSeconds <int>]
[-Quiet]
[<CommonParameters>]
Test-Connection
[-TargetName] <string[]>
-TcpPort <int>
[-IPv4]
[-IPv6]
[-ResolveDestination]
[-Source <string>]
[-TimeoutSeconds <int>]
[-Quiet]
[<CommonParameters>]
In the above syntax, you can use one set at a time like you can’t use the -TCPPort and -TraceRoute parameter together.
Using Ping Command:
ping [/t] [/a] [/n <count>] [/l <size>] [/f] [/I <TTL>] [/v <TOS>] [/r <count>] [/s <count>] [{/j <hostlist> | /k <hostlist>}] [/w <timeout>] [/R] [/S <Srcaddr>] [/4] [/6] <targetname>
How does PowerShell Ping Works?
Ping command was introduced in the command prompt and can also be used with the PowerShell as well. Ping command uses the ICMP (Internet Control Protocol Message) packets to send over the remote nodes and checks the connectivity in return by receiving the echo packets back from the remote nodes. Ping command also determines the name of the host in the domain systems if the DNS record is configured properly. PowerShell has introduced a more powerful command than ping, which is called the Test-Connection. It is a combination of TestConnection + PingStatus.
To ping the remote computer, we can simply use:
Code:
ping google.com
Output:
Using the Test-Connection Command.
Code:
Test-Connection Google.com
Output:
Examples of PowerShell Ping
Given below are the examples of PowerShell Ping:
Example #1 – Using the ping command to determine the Hostname.
If you want to retrieve the remote server hostname, you can use the ping command with the -a parameter to determine that.
Code:
ping -a 192.168.0.200
Output:
The -a parameter retrieves the computer name from the IP address (PTR) record.
Example #2 – Retrieving ping continuously.
Sometimes we need to check the remote server status continuously, and in that case, you can use the -t parameter, so it returns the echo result until the Ctrl+C button is pressed.
Code:
ping -a 192.168.0.206 -t
Output:
Example #3 – Using -Quiet mode with the Test-Connection command.
-Quiet parameter suppress the output that the Test-Connection command generates and returns the output in the Boolean form. If the remote connectivity establishes successfully, it returns the true otherwise false.
Code:
Test-Connection -ComputerName 192.168.0.200 -Quiet
Output:
False output example,
Output:
Example #4 – Restricting count with the -count parameter.
By default, the Test-Connection command generates the 4 counts. However, you can restrict the number you want.
Code:
Test-Connection -ComputerName 192.168.0.200 -Count 2
Output:
This is particularly helpful when we write a script. For example, we can combine -Count and -Quiet parameters so that we get the output quickly.
Code:
Test-Connection -ComputerName 192.168.0.200 -Count 1 -Quiet
Output:
In the script, we can use the If else condition.
Code:
if(Test-Connection -ComputerName 192.168.0.200 -Count 1 -Quiet){
Write-Output "Server Connectivity Successful"
}
else{
Write-Output "Server Connectivity failed"
}
Output:
Example #5 – Using the Test-Connection for multiple servers.
The -ComputerName parameter in the Test-Connection command accepts multiple computer names because it is a string array.
Code:
Test-Connection -ComputerName 192.168.0.200, 192.168.0.206, 192.168.0.230 -Count 1 | ft -AutoSize
Output:
We can use it inside the try/catch block to catch the error.
Code:
Try {
Test-Connection -ComputerName 192.168.0.200, 192.168.0.206, 192.168.0.230 -Count 1 -EA Stop | ft -AutoSize
}
Catch {
$_.Exception.Message
}
Output:
Example #6 – Using -TraceRoute to determine the hopes of the destination node.
We can add -TraceRoute parameter to determine the route to the destination node.
Code:
Test-Connection -TargetName www.google.com -Traceroute
Output:
Example #7 – Setting up custom buffer size and delay count.
We can add the custom buffer size and set the delay in seconds between consequent requests, as shown below.
Code:
Test-Connection -ComputerName 192.168.0.200 -Delay 3 -BufferSize 256
Output:
Example #8 – Running Test-Connection as a background job.
We can run the Test-Connection command as the background job using the -AsJob parameter.
Code:
Test-Connection -ComputerName 192.168.0.200, 192.168.0.206 -Count 2 -AsJob
Output:
Once you get the Job Name, you can use the Receive-Job command to retrieve the status of the job.
Code:
Receive-Job -Name Job5
Output:
Example #9 – Using multiple sources to send a request to the remote computer.
You can use multiple sources to send a request to the remote computer.
Code:
Test-Connection -Source 192.168.0.202, 192.168.0.206 -ComputerName 192.168.0.200 -Count 2
Output:
Example #10 – Using the different credentials.
You can use the different credentials if the source and remote computers are in different domains.
Code:
Test-Connection -Source 192.168.0.206 -ComputerName 192.168.0.200 -Count 2 -Credential Automationlab\Administrator
Output:
Conclusion
The PowerShell ping command is one of the useful commands when we write a script. It is the first step to determine if the remote server is online or not. Many times there are possibilities that the server has blocked the ICMP request in the firewall; in that case, you can use the commands like Test-WSMan to check if the WINRM connectivity works or not.
Recommended Articles
This is a guide to PowerShell Ping. Here we discuss the introduction; how does PowerShell ping works? And examples, respectively. You may also have a look at the following articles to learn more –