title: How to determine the port on which a Windows service is listening?
body: <p>I have running a Windows service called <code>my-service</code>. The only input that I know is the <strong>service name</strong>.</p>
  <p>I can get the <strong>process identifier</strong> by using the <strong>service name</strong>:</p>
  <pre><code>$id = Get-WmiObject -Class Win32_Service -Filter &quot;name='my-service'&quot; | Select-Object -ExpandProperty ProcessId
  </code></pre>
  <p>To get a list of Listening ports, I can use <code>netstat</code>, but all my custom services have <code>PID</code> <strong>4</strong> which is not equal to the service <strong>process identifier</strong> of <code>Get-WmiObject</code> cmdlet.</p>
  <p><a href="https://i.stack.imgur.com/Hxy6U.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Hxy6U.png" alt="netstat.exe -ano" /></a></p>
  <p>The goal is to get the port on which the service is listening by using PowerShell or CMD based on the <strong>service name</strong> or the <strong>process identifier</strong> and NOT by the <strong>image name</strong>.</p>