I was trying to check whether the port is opened or not using powershell like follows.
(new-object Net.Sockets.TcpClient).Connect("10.45.23.109", 443)
This method works , but the output is not user-friendly. It means if there are no errors then it has access. Is there any way to check for success and display some message like " Port 443 is operational"?
One of the simplest ways to check for open ports is to use NetStat.exe. You can find this tool in the System32 folder on Windows 10. With NetStat, you can see open ports or ports that a specific host uses. Netstat is short for network statistics.
Type "Network Utility" in the search field and select Network Utility. Select Port Scan, enter an IP address or hostname in the text field, and specify a port range. Click Scan to begin the test. If a TCP port is open, it will be displayed here.
You can use netstat command to list the tcp port, if 443 port is listed there and state is established means 443 is open for outbound communication.
On a Windows computerPress the Windows key + R, then type "cmd.exe" and click OK. Enter "telnet + IP address or hostname + port number" (e.g., telnet www.example.com 1723 or telnet 10.17.xxx.xxx 5000) to run the telnet command in Command Prompt and test the TCP port status.
If you're running Windows 8/Windows Server 2012 or newer, you can use the Test-NetConnection command in PowerShell.
Ex:
Test-NetConnection -Port 53 -ComputerName LON-DC1
I improved Salselvaprabu's answer in several ways:
Call it like this:
Test-Port example.com 999 Test-Port 192.168.0.1 80
function Test-Port($hostname, $port) { # This works no matter in which form we get $host - hostname or ip address try { $ip = [System.Net.Dns]::GetHostAddresses($hostname) | select-object IPAddressToString -expandproperty IPAddressToString if($ip.GetType().Name -eq "Object[]") { #If we have several ip's for that address, let's take first one $ip = $ip[0] } } catch { Write-Host "Possibly $hostname is wrong hostname or IP" return } $t = New-Object Net.Sockets.TcpClient # We use Try\Catch to remove exception info from console if we can't connect try { $t.Connect($ip,$port) } catch {} if($t.Connected) { $t.Close() $msg = "Port $port is operational" } else { $msg = "Port $port on $ip is closed, " $msg += "You may need to contact your IT team to open it. " } Write-Host $msg }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With