EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell test-connection
Secondary Sidebar
PowerShell Tutorial
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

PowerShell test-connection

PowerShell test-connection

Introduction to PowerShell test-connection

Test-Connection cmdlet in a PowerShell sends Internet Control Message Protocol (ICMP) echo request packets to the remote computers and, in return, receives the echo-response to check if the remote computers are alive or in the network or can be connected. Unlike the familiar ping command, this command Tests the connections, sets the buffersize, number of hope, throttle limits, the delay between each request, and includes the ping status.

Syntax

Below is the syntax:

Pair 1:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Test-Connection
[-TargetName] <string[]>
[-Ping] [-IPv4] [-IPv6] [-ResolveDestination] [-Source <string>] [-MaxHops <int>] [-Count <int>] [-Delay <int>] [-BufferSize <int>] [-DontFragment] [-TimeoutSeconds <int>] [-Quiet] [<CommonParameters>]

Pair2:

Test-Connection
[-TargetName] <string[]>
-Repeat
[-Ping] [-IPv4] [-IPv6] [-ResolveDestination] [-Source <string>] [-MaxHops <int>] [-Delay <int>] [-BufferSize <int>] [-DontFragment] [-TimeoutSeconds <int>] [-Quiet] [<CommonParameters>]

Pair3:

Test-Connection
[-TargetName] <string[]>
-MtuSize
[-IPv4] [-IPv6] [-ResolveDestination] [-TimeoutSeconds <int>] [-Quiet] [<CommonParameters>]

Pair4:

Test-Connection
[-TargetName] <string[]>
-Traceroute
[-IPv4] [-IPv6] [-ResolveDestination] [-Source <string>] [-MaxHops <int>] [-TimeoutSeconds <int>] [-Quiet] [<CommonParameters>]

Pair5:

Test-Connection
[-TargetName] <string[]>
-TcpPort <int>
[-IPv4] [-IPv6] [-ResolveDestination] [-Source <string>] [-TimeoutSeconds <int>] [-Quiet] [<CommonParameters>]

Please note: We need to use a single pair at a time as a syntax. We can’t use the combination of pairs. For example, We can’t use Mtusize and TraceRoute parameters together.

How does the Test-Connection command work in PowerShell?

This command’s sole purpose is to check if the remote computers are online or not by sending them ICMP requests to the remote hosts. Remote computer response depends on offline, their firewall rules, network security rules, etc.

Many times remote computers are online, but they cannot respond due to the above-mentioned reasons. This command is far better than the ping command as we can play with the different parameters like we can specify the number of count in response, can send requests for the multiple servers, set the buffer size and MTU limit, etc.

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,650 ratings)

Examples

Different examples are mentioned below:

Example #1 – Test-Connection for a remote computer.

This example simply explains remote connectivity.

Test-Connection -ComputerName LabMachine2k12

Output:

PowerShell test-connection output 1

In the above example, Source mentions the originated machine, the destination is the remote computer, IP address is the remote computer IP.

For PowerShell core version (version 6.0+) ComputerName parameter is renamed to TargentName.

Example #2 – Test-Connection for multiple remote computers.

To check the connectivity of the multiple remote computers, we can provide the multiple computer names for the -ComputerName parameter because this parameter accepts the string array.

help Test-Connection -Parameter ComputerName

Output:

PowerShell test-connection output 2

You can check the ComputerName parameter is a String array parameter.

Example #3 – Test-Connection with -Count parameter.

We can specify the number of counts we need to receive as a response from the remote computer using the -Count Parameter. The default number of responses we receive from remote computers is 4.

Test-Connection -ComputerName LabMachine2k12 -Count 2

Output:

PowerShell test-connection output 3

This parameter is generally used when we test connections for multiple computers. If we keep the default parameter, it counts for 4, and checking remote connectivity for multiple servers will take more than the expected time.

Example #4 – Test-Connection with -Quiet parameter.

The -Quiet parameter returns the output in Boolean (True or False). If the server is reachable, the output is true; otherwise, the output is false.

For example,

Test-Connection -ComputerName LabMachine2k12 -Quiet

Output:

output 4

If the remote system isn’t reachable,

output 5

Most of the time, programmers use both -Count and -Quiet parameters together to check multiple server’s connectivities. For example, we have a Servers.txt file containing the list of servers, and to check if the server is reachable or not we will use the below code.

foreach($server in (Get-Content C:\temp\Servers.txt)){
if(Test-Connection -ComputerName $server -Count 2 -Quiet){Write-Output "$server is Online"}
else{Write-Output "Server is offline"}
}

Example #5 – Establishing a session if connection successful

From the above examples reference, you can also establish a session with the remote server only when the connection is successful. For example,

if(Test-Connection -ComputerName 'LabMachine2k12' -Count 2 -Quiet){
New-PSSession -ComputerName 'LabMachine2k12'
}

Output:

output 6

Example #6 – Using -Delay Parameter

We can use the delay parameter and provide values in seconds. This parameter specifies the delay in seconds between each request.

In the below example, there will be 2 requests sent to the remote servers with an interval of 5 seconds.

Test-Connection -ComputerName LabMachine2k12 -Delay 5 -count 2

The default delay is 1 second, and however, the delay parameter is not recommended unless using for any investigation or troubleshooting purpose because it delays the PowerShell operation.

Example #7 – Test-Connection with BufferSize.

BufferSize parameter specifies the size in bytes of the buffer sent with the Test-Connection command. The default value is 32 if not specified. This is an integer parameter.

Test-Connection -ComputerName LabMachine2k12 -BufferSize 64

Output:

output 7

If the buffer size is not specified, the output will produce 32 (Default value) in the Bytes field. We can use this parameter for the high traffic network condition.

Example #8 – Using ThrottleLimit parameter.

ThrottleLimit is the integer parameter and specifies the number of concurrent connections to establish. Its default value is 32, but we can change this value for more concurrent connections.

Test-Connection -ComputerName LabMachine2k12 -ThrottleLimit 50

We can use both buffersize and throttlelimit parameters together.

Test-Connection -ComputerName LabMachine2k12 -BufferSize 64 -ThrottleLimit 50

Example #9 – Test-Connection with different credentials.

If the remote server(s) are not in the same domain, we can connect use remote server credentials to test the connection. For example,

$creds = Get-Credential
Test-Connection -ComputerName LabMachine2k12 -Credential $creds -Count 2

Example #10 – Test-Connection with TTL parameter.

TimeToLive (Alias: TTL) specifies the maximum number of times the packets can be forwarded. For every hope in the gateway, routers, etc. Each time the TTL value is decreased by one, and when it becomes 0, the error is returned. Its default value is 128.

You can change the TTL value using the parameter as shown below.

Test-Connection -ComputerName LabMachine2k12 -TimeToLive 200

If there are more hopes, you can increase the TTL value.

Conclusion

Test-Connection is the first step and very crucial step in the PowerShell script as it saves a lot of time running the remote commands because they can be executed only when this command returns True value. In some organizations, the Firewall blocks the ICMP port for security reasons, and in that case, we can also determine by using WINRM connectivity using the Test-WSMan command if the remote computer is reachable or not.

Recommended Articles

This is a guide to PowerShell test-connection. Here we discuss How does the Test-Connection command work in PowerShell, along with the examples. You may also have a look at the following articles to learn more –

  1. PowerShell SubString
  2. PowerShell Filter
  3. PowerShell Delete File
  4. PowerShell Registry
Popular Course in this category
PowerShell Training (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |   4+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more