Search code examples
powershellportpowershell-2.0port-scanning

How to check Network port access and display useful message?


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"?


Solution

  • Actually Shay levy's answer is almost correct but i got an weird issue as i mentioned in his comment column. So i split the command into two lines and it works fine.

    $Ipaddress= Read-Host "Enter the IP address:"
    $Port= Read-host "Enter the port number to access:"
    
    $t = New-Object Net.Sockets.TcpClient
    $t.Connect($Ipaddress,$Port)
        if($t.Connected)
        {
            "Port $Port is operational"
        }
        else
        {
            "Port $Port is closed, You may need to contact your IT team to open it. "
        }