Updated March 10, 2023
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.
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:
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:
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:
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 #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:
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 –