Skip to main content

How to use Test-NetConnection in PowerShell

In the latest versions of Windows, the client and telnet service are not installed by default. They are available as installable functions. Unfortunately, turning on telnet often requires a restart computer. The connection test can be performed with the ping command, but does not perform any tracing or port testing. In most network problems we need to use ping command with tracert. Also telnet for check for open ports.


PowerShell Test-NetConnection

The Test-NetConnection command is the successor to Test-Connection but provides more information. Let's do a simple test to Google:

PS C:\> Test-NetConnection 8.8.8.8 

ComputerName           : 8.8.8.8
RemoteAddress          : 8.8.8.8
InterfaceAlias         : Ethernet 2
SourceAddress          : 192.168.1.10
PingSucceeded          : True
PingReplyDetails (RTT) : 9 ms

The command returns a lot of information, usually this is enough to find a network problem (if any). It will display the interface, source IP and if the ping was successful.

Detailed Informations

If we need first hop in the lookup (nslookup), we can add the -InformationLevel "Detailed" parameter to the command.

PS C:\> Test-NetConnection 8.8.8.8 -InformationLevel Detailed

ComputerName           : 8.8.8.8
RemoteAddress          : 8.8.8.8
NameResolutionResults  : 8.8.8.8
                         dns.google
InterfaceAlias         : Ethernet 2
SourceAddress          : 192.168.1.10
NetRoute (NextHop)     : 192.168.1.1
PingSucceeded          : True
PingReplyDetails (RTT) : 9 ms

Simple test connection (like ping)

Or if we need a simple connection test to the target IP address or website we can use -InformationLevel "Quiet" which will return us a bollean value.

PS C:\> Test-NetConnection google.com -InformationLevel Quiet

True

Test open TCP port

I wrote at the beginning of the post that an open port usually checks with telnet. The Test-NetConnection command can also check whether a specific port is open or not. Just add the -Port parameter with number of port to the command.

PS C:\> Test-NetConnection google.com -Port 80
ComputerName     : google.com
RemoteAddress    : 172.217.16.14
RemotePort       : 80
InterfaceAlias   : Ethernet 2
SourceAddress    : 192.168.1.10
TcpTestSucceeded : True


PS C:\> Test-NetConnection google.com -Port 21
WARNING: TCP connect to (172.217.16.14 : 21) failed

ComputerName           : google.com
RemoteAddress          : 172.217.16.14
RemotePort             : 21
InterfaceAlias         : Ethernet 2
SourceAddress          : 192.168.1.10
PingSucceeded          : True
PingReplyDetails (RTT) : 10 ms
TcpTestSucceeded       : False

As you can see, port 80 is open but 21 is closed. You can notice an interesting thing - if the port is closed, the command will test if the ping is successful.

Comments

Popular posts from this blog

PowerShell Permanently Clear History

PowerShell has a command to clear the command history like Clear-History. However, it is independent of the host and must be deleted separately. This information is especially important when you need to use a command with your password (in command).  How to permanently delete PowerShell history? The entire history of the commands you use in PowerShell is saved to one file. To fully delete the history, all you need to do is ... delete the file. Usually the file is located in AppData, but you can check the exact path with the command which will display the full path. PS C:\> (Get-PSReadlineOption).HistorySavePath ### result C:\Users\geekitsupp\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt After deleting this file, all history of commands executed in PowerShell will be deleted. Clear-History or [Microsoft.PowerShell.PSConsoleReadLine]::ClearHistory() command doesn't permanently delete history. They can only delete ...

Keyboard Shortcut for Part or Full Screenshot

Windows 10 has a built-in screenshot tool. You can take a screenshot of the full screen or just a part of it. The screenshots will be copied to the clipboard so you can paste into any program that supports images like Paint, Photoshop, messengers like MS Teams or email messages.  Keyboard Shortcut Press Winkey + Shift + S . The screen will have a light or dark overlay and the mouse cursor will change to a plus (+) symbol, which means capture mode is on. A bar will appear at the top of the main screen. From there, you can choose one of the snipping modes: Rectangular  - take a rectangular screenshot by dragging the desired fragment with the cursor. Freeform  - freely create a screenshot of a non-standard shape by "painting" with the pressed cursor. Window  - captures part of the screen, e.g. a window on the screen, such as browser window, file explorer window, etc. Fullcreen  - captures ...