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 Test-NetConnection
 

PowerShell Test-NetConnection

Updated March 10, 2023

PowerShell-Test-NetConnection

 

 

Introduction to PowerShell Test-NetConnection

The test-net connection is used to various information that are associated with a connection such as diagnostic and connectivity. Before PowerShell v4.0, there were different tools that are used for troubleshooting different issues related to the network. To avoid that, this cmdlet was introduced as a single point of source for troubleshooting various connectivity-related issues. The various diagnostics that are supported by this cmdlet are route tracing and selection diagnostics, ping, and TCP. Based on the input supplied, the output ranges from a variety of information such as lookup results of DNS, various IP addresses and security rules configured, source and destination IP address selection results, and information related to the connection. This article will cover in detail the test-net connection, its parameters, and its usage in detail.

Watch our Demo Courses and Videos

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

Syntax:

NAME
Test-NetConnection

Syntax:

Test-NetConnection [[-ComputerName] <string>] [-TraceRoute] [-Hops <int>] [-InformationLevel {Quiet | Detailed}] [<CommonParameters>] Test-NetConnection [[-ComputerName] <string>] [-CommonTCPPort] {HTTP | RDP | SMB | WINRM} [-InformationLevel {Quiet | Detailed}] [<CommonParameters>] Test-NetConnection [[-ComputerName] <string>] -Port <int> [-InformationLevel {Quiet | Detailed}]  [<CommonParameters>] Test-NetConnection [[-ComputerName] <string>] -DiagnoseRouting [-ConstrainSourceAddress <string>] [-ConstrainInterface <uint32>] [-InformationLevel {Quiet | Detailed}]  [<CommonParameters>] ALIASES
TNC

Eg:

Input:

Test-NetConnection

Output:

PowerShell Test-NetConnection 1

Parameters of PowerShell Test-NetConnection

  • CommonTcpPort:

This denotes the tcp port number to be checked. The datatype of this parameter is string. This is not a mandatory parameter. The position of the parameter in this cmdlet is 1. It doesn’t accept pipline and wildcard as input. The only accepted values for this parameter are SMB, HTTP, WINRM, and HTTP.

  • ComputerName:

This specifies the target system’s DNS or IP Address. The data type of this parameter is string. It can be referred also using RemoteAddress, cn. The position of this parameter in the cmdlet is zero. Default value is none. It accepts pipeline input whereas wild card characters are not permitted.

  • -ConstrainInterface:

This denotes the mechanism that used to be for diagnosing routes. The data type of this parameter is Uint32. The default value is none. It doesn’t accept both pipeline and wild card characters as input.

  • -ConstrainSourceAddress:

This denotes the constraint to be used for diagnosing source addresseses. The data type of this parameter is string. The default value is none. It doesn’t accept both pipeline and wild card characters as input.

  • -DiagnoseRouting:

This denotes the diagnostics ran from source to destination. The data type of this parameter is string. The default value is none. It doesn’t accept both pipeline and wild card characters as input.

  • -Hops:

This denotes the number of jumps that should performed as part of traversing the trace route cmdlet. The data type of this parameter is int32. The default value is none. It doesn’t accept both pipeline and wild card characters as input.

  • -InformationLevel:

This denotes the type of information that should be displayed. The data type of this parameter is string. The default value is none. It doesn’t accept both pipeline and wild card characters as input.The following are the values that can be supplied to this parameter; Detailed and quiet. If the value is quiet, only basic information is returned.

  • -Port:

This denotes the port number of the remote computer to check for connection. It can also be referred to using Remoteport. The data type of this parameter is int32. The default value is none. It accepts pipeline input, but wild card characters are not allowed.

  • -Traceroute:

This denotes the connectivity test that is run on the destination computer. The data type of this parameter is switch. The default value is none It doesn’t accept both pipeline and wild card characters as input.

Before Test-Netconnection, the previous version was Test-Connection. It has parameters such as buffersize and delay that can be used to define the seconds between successive pings.

Examples

Let us discuss examples of PowerShell Test-NetConnection.

Example #1: Getting detailed and quiet information

Input:

Write-Host "Welcome to demo of test-netconnection" -ForegroundColor Green
Write-Host "Running the command with informational level"
$infolevel=Read-Host "specify the information level"
Write-Host "The speified information level is" $infolevel -ForegroundColor Green
Test-NetConnection -InformationLevel $infolevel
$infolevel=Read-Host "specify the information level"
Write-Host "The speified information level is" $infolevel -ForegroundColor Green
Test-NetConnection -InformationLevel $infolevel

Output:

PowerShell Test-NetConnection 2

Quiet information level returns only a binary value. If the connection is successful true is returned and if the connection doesn’t exist then false is returned.

Example #2

To check remote connection using port number and pinging a remote server

Input:

Write-Host "testing conenction using port number" -ForegroundColor Green
$infolevel= Read-Host "Enter the information level to be used"
$portno= Read-Host "Enter the port no to check"
Write-Host "The details are as follows" -ForegroundColor Green
Test-NetConnection -Port $portno -InformationLevel $infolevel
Write-Host "Demo of reching out to remote computer"
$remotecmp= Read-Host "Enter the remote computer details"
$infolevel1= Read-Host "Enter the info level to be used"
Write-Host "The details are as follows" -ForegroundColor Green
Test-NetConnection -ComputerName $remotecmp -InformationLevel $infolevel1

Output:

PowerShell Test-NetConnection 3

Example #3

Input:

Write-Host "Demo to see if a port is open" -ForegroundColor Green
$rs= Read-Host "enter the remote server"
Test-NetConnection -ComputerName $rs -CommonTCPPort HTTP
$portno=Read-Host "enter the port no"
$ipadd= Read-Host "enter the ip address"
$con = New-Object System.Net.Sockets.TcpClient($ipadd, $portno)
if ($connection.Connected) {
Write-Host "connection is succeeded" -ForegroundColor Green
} else {
Write-Host "connection is failed" -ForegroundColor Red
}

Output:

example 3

Example #4

Input:

Write-Host "Demo of trace route in test-netonnection"
$servername= Read-Host "enter the server name"
Test-NetConnection -ComputerName $servername -TraceRoute
Write-Host "With detailed information" -ForegroundColor Green
Test-NetConnection -ComputerName $servername -TraceRoute -InformationLevel Detailed
Write-Host "demo of diagnostic routing"
$sname= Read-Host "enter the site address"
Test-NetConnection -ComputerName $sname -DiagnoseRouting
Write-Host "With detailed information" -ForegroundColor Green
Test-NetConnection -ComputerName $sname -DiagnoseRouting -InformationLevel Detailed

Output:

output

output 1

Conclusion

Thus, the article explained in detail the Test-NetcOnnection cmdlet in detail. It covered various scenarios in which it can be used with appropriate examples. It also explained the various parameters that are associated with the cmdlet. To learn more in detail it is advisable to write and execute sample scripts.

Recommended Articles

This is a guide to PowerShell Test-NetConnection. Here we discuss the Introduction, syntax, examples with code implementation. You may also have a look at the following articles to learn more –

  1. PowerShell Wait
  2. PowerShell change directory
  3. PowerShell where
  4. PowerShell uninstall module

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