Query Port Information using Powershell


Introduction:

Have you ever googled a port number to figure out what service uses it? I think every system administrator must have done this. But how to approach when there are a lot of port numbers to search?
So, I thought giving it a try with PowerShell, to programmatical get port information of lots of port numbers.

The Idea and Making it work:

  1. First get information for all the ports, but where to get it? I read somewhere during my engineering graduation days, that port numbers are assigned by IANA (Internet Assigned Numbers Authority). How about checking their website first.So I found a link on IANA’s website which has port numbers in a CSV format and downloaded the file.

     

  2. Once you have the information, filter out all unnecessary things and store them in a CSV or JSON format, so that it can be easily uploaded/downloaded from a Public Github gist.Using the below script I filtered out the required information and pasted/committed it to my Github repository  , from where it could be downloaded to any machine since it is Public.

    Import-Csv "C:\Users\prateeksingh\Downloads\service-names-port-numbers.csv"|`
    Where-Object {$_.'Port Number'} |select @{n='Port';e={$_.'Port number'}},`
    @{n='Service';e={$_.'Service Name'}},`
    @{n='Protocol';e={$_.'Transport Protocol'}},`
    'Description'|ConvertTo-Csv -NoTypeInformation|clip
  3. Write a Powershell function that can
    1. Web request above mentioned public Github gist URL and downloads the content.
    2. Filter out port number you’re trying to search from the content, and display the results

      Following is the PowerShell function to get above done

      <#
      .Synopsis
      Gets information of a port number
      .DESCRIPTION
      Function provides detailed information of port numbers, like - the service which use the port, Transport protocol and a small decsription.
      .EXAMPLE
      PS > Get-Port -Port 20,21,53
      Port Service Protocol Description
      ---- ------- -------- -----------
      20 ftp-data tcp File Transfer [Default Data]
      20 ftp-data udp File Transfer [Default Data]
      20 ftp-data sctp FTP
      21 ftp tcp File Transfer Protocol [Control]
      21 ftp udp File Transfer Protocol [Control]
      21 ftp sctp FTP
      53 domain tcp Domain Name Server
      53 domain udp Domain Name Server
      .EXAMPLE
      PS > 135, 25, 23 | Get-Port
      Port Service Protocol Description
      ---- ------- -------- -----------
      135 epmap tcp DCE endpoint resolution
      135 epmap udp DCE endpoint resolution
      25 smtp tcp Simple Mail Transfer
      25 smtp udp Simple Mail Transfer
      23 telnet tcp Telnet
      23 telnet udp Telnet
      .EXAMPLE
      PS > port 389
      Port Service Protocol Description
      ---- ------- -------- -----------
      389 ldap tcp Lightweight Directory Access Protocol
      389 ldap udp Lightweight Directory Access P
      #>
      Function Get-Port
      {
      [CmdletBinding(HelpUri ='https://geekeefy.wordpress.com/2017/05/09/query-port-information-using-powershell/&#39;)]
      [Alias('port')]
      [OutputType([psobject])]
      Param
      (
      [Parameter(
      Mandatory=$true,
      ValueFromPipeline=$true,
      ValueFromPipelineByPropertyName=$true,
      Position=0,
      HelpMessage="Please provide a port number")]
      [ValidateRange(0,65536)][int[]]$Port
      )
      Begin
      {
      $TempFileName = "$env:TEMP\Ports.csv"
      If(Test-Path $TempFileName){
      $Data = Import-Csv $TempFileName
      }
      else{
      $Data = Invoke-WebRequest 'https://goo.gl/G1i6RU&#39; | `
      ForEach-Object content | `
      Tee-Object -FilePath $TempFileName -Verbose | ConvertFrom-Csv
      }
      }
      Process
      {
      $Port | ForEach-Object {
      $pv= $_
      $Data.Where({$_.port -eq $pv})
      }
      }
      End
      {
      Remove-Variable -Name Data
      [gc]::Collect()
      }
      }
      view raw Get-Port.ps1 hosted with ❤ by GitHub
  4. Make sure when the Powershell function runs for the first time, it creates a local copy of content in a File at a static location, which could be accessed to get port information locally for all Future function calls by avoiding the Web request which is slower.Took care of point-6 in my PowerShell function like in the screenshot belowcnd

Running the Function:

You can run the function on demand like in the below screenshot.

Please Note – The First call to the function would be slow, but from the next call you have a local copy of the CSV file on your machine, which can be queried comparatively much faster to web requests.

1

Hope you’ll find the script useful and Thanks for reading, Cheers! 🙂

signature

10 thoughts on “Query Port Information using Powershell

  1. Thank you for sharing superb informations. Your web-site is fantastically cool. I am impressed by the details that you have proceeding this web site. It reveals how nicely you perceive this subject. Bookmarked this web call, will be as long as backside in support of more articles. You, my friend, ROCK! I originate simply the info I previously searched all more than the place and just couldn’t extend athwart. Could you repeat that? an ideal trap site.

    Like

Leave a reply to 58sniper Cancel reply