# Which Powershell Function could check whether a certain port is in LISTENING state on localhost?

Introduction

Aim: to check whether a port is in LISTENING state on localhost by using a PowerShell function

The following command:

New-Object Net.Sockets.TcpClient.Connect("127.0.0.1",10389)

results in:

PS C:\Windows\system32> New-Object Net.Sockets.TcpClient.Connect("127.0.0.1",10389)
At line:1 char:33
+ New-Object Net.Sockets.TcpClient.Connect("127.0.0.1",10389)
+                                 ~
Unexpected token ')' in expression or statement.
+ CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken

PS C:\Windows\system32>


Question

Which PowerShell Function could be used to check whether a certain port is in LISTENING state on localhost?

-
Are you trying to see if the port on the local machine is in LISTENING state, or are you trying to catch the error when the TCPClient.Connect fails to connect to a host? These are two very different things. :) PS: try adding brackets: (New-Object Net.Sockets.TcpClient).Connect("127.0.0.1",10389), as this will change the order of operations and instantiate the new socket object before calling Connect on it. Or split it up into two separate commands using a variable. –  Ƭᴇcʜιᴇ007 Jun 17 at 17:17
Question has been updated. –  utrecht Jun 17 at 17:19

If you're using PowerShell v3.0+, then instead of trying to connect to the port to determine the state, you can simply use Get-NetTCPConnection:

Get-NetTCPConnection -State Listen

To me this is more accurate as it's reading the status of the port on the computer. Using a connection to test can make it seem like it's not "LISTENING" when it is, if a firewall or alike gets in the way or something.

-
Get-NetTCPConnection –LocalPort 10389 works perfect on Windows8. Thank you. –  utrecht Jun 17 at 17:30

First create and store the connection:

$connection = (New-Object Net.Sockets.TcpClient)$connection.Connect("127.0.0.1",10389)


Then check if it's connected

if ($connection.Connected) { "We're connected" }  Or as suggested by Colyn1337 Try {$connection = (New-Object Net.Sockets.TcpClient)
$connection.Connect("127.0.0.1",10389) "Connected" } Catch { "Can't Connect" }  - I'd recommend using a try/catch/finally block to handle a terminating error if the connection is refused. – Colyn1337 Jun 17 at 17:23 PS C:\Windows\system32> . "C:\path\to\file.ps1" Connected PS C:\Windows\system32> net stop apacheds The apacheds service is stopping. The apacheds service was stopped successfully. PS C:\Windows\system32> . "C:\path\to\file.ps1" Can't Connect PS C:\Windows\system32> Works well. Thank you – utrecht Jun 17 at 17:35 I had tried to add this to techie's answer, so this is an expansion of his. You can fine tune the output like so: Get-NetTCPConnection -State Listen | Where-Object {$_.LocalAddress -eq "192.168.56.1" -and \$_.LocalPort -eq "139"}


That would return an array of data if it were listening on that port. If there is no listener, it returns null and therefore no need for error handling.

-