Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.


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("",10389)

results in:

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

PS C:\Windows\system32>


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

share|improve this question
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("",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 '14 at 17:17
Question has been updated. –  Utrecht Jun 17 '14 at 17:19

4 Answers 4

up vote 4 down vote accepted

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.

share|improve this answer
Get-NetTCPConnection –LocalPort 10389 works perfect on Windows8. Thank you. –  Utrecht Jun 17 '14 at 17:30

First create and store the connection:

$connection = (New-Object Net.Sockets.TcpClient)

Then check if it's connected

if ($connection.Connected) {
    "We're connected"

Or as suggested by Colyn1337

Try {
    $connection = (New-Object Net.Sockets.TcpClient)
Catch {
    "Can't Connect"
share|improve this answer
I'd recommend using a try/catch/finally block to handle a terminating error if the connection is refused. –  Colyn1337 Jun 17 '14 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 '14 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 "" -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.

share|improve this answer

i was all set to give one of the Get-NetTCPConnection answers an upvote, after all it worked on my machine. Sadly it did not work on the Windows 2008 servers that were running Powershell 3.0 that had to do the check.

However the following worked perfectly

([Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()).GetActiveTcpListeners() | Where Port -EQ 10389

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.