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

4

3

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?

030

Posted 2014-06-17T17:11:21.113

Reputation: 1 924

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 – 2014-06-17T17:17:45.253

Question has been updated. – 030 – 2014-06-17T17:19:32.450

Answers

8

If you're using PowerShell v3.0+ on Windows 8/Server 2012 or later, 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.

Ƭᴇcʜιᴇ007

Posted 2014-06-17T17:11:21.113

Reputation: 103 763

Get-NetTCPConnection –LocalPort 10389 works perfect on Windows8. Thank you. – 030 – 2014-06-17T17:30:49.177

2

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

Tim Ferrill

Posted 2014-06-17T17:11:21.113

Reputation: 588

1I'd recommend using a try/catch/finally block to handle a terminating error if the connection is refused. – Colyn1337 – 2014-06-17T17:23:47.467

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 – 030 – 2014-06-17T17:35:20.187

1

If you are checking that Port number is occupied or not then normal if -eq “$null” doesn’t work:

For example if you are running following command

$PortNumberOutPut = Get-NetTCPConnection | where Localport -eq $portNumber

You want to check if variable is null

if (!$PortNumberOutPut) { Write-Host "variable is null" }

And if you want to check if $PortNumberOutPut has any value except $null

if ($PortNumberOutPut) { Write-Host "variable is NOT null" }

Rudresh Bhatt

Posted 2014-06-17T17:11:21.113

Reputation: 111

1

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.

Colyn1337

Posted 2014-06-17T17:11:21.113

Reputation: 1 191

1

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

rob

Posted 2014-06-17T17:11:21.113

Reputation: 575