EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Ping
 

PowerShell Ping

Updated March 13, 2023

PowerShell Ping

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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.

Note: Few parameters were added to the latest version of the PowerShell. The above parameters are supported in PowerShell 7.1.3. If you need to check the older version of this command, you can check on the Microsoft documentation.

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:

PowerShell Ping 1

Using the Test-Connection Command.

Code:

Test-Connection Google.com

Output:

PowerShell Ping 2

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:

PowerShell Ping 3

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:

PowerShell Ping 4

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:

-Quiet mode with the Test-Connection command

False output example,

PowerShell Ping 6

Output:

PowerShell Ping 7

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:

PowerShell Ping 8

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:

PowerShell Ping 9

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:

PowerShell Ping 10

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:

Test-Connection for multiple servers

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:

try/catch block to catch the error

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:

Using -TraceRoute to determine the hopes of the destination node

Note: These two parameters (-TraceRoute and -TargetName) are not supported in the PowerShell Framework version (up to PowerShell v5.1).

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:

Setting up custom buffer size and delay count

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:

PowerShell Ping 15. JPG

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:

PowerShell Ping 16. JPG

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:

multiple sources to send a request to the remote computer

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:

PowerShell Ping 18. JPG

PowerShell Ping 19. JPG

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 –

  1. PowerShell Sleep
  2. PowerShell SubString
  3. PowerShell not like
  4. Else If in PowerShell

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - ENROLL NOW