Is there a way in powershell to check if a given port is in use or not?
Asked
Active
Viewed 2.8k times
20
-
have you tried searching for "powershell port" ? – Mitch Wheat Aug 02 '11 at 10:07
-
1Do you mean TCP/IP Port? – Richard Aug 02 '11 at 10:08
-
Just searching over google, found [this](http://dthomo.wordpress.com/2011/03/14/powershell-to-check-tcp-port-is-open/). may b it help. – Saad Aug 02 '11 at 10:08
4 Answers
38
Get-NetTCPConnection | where Localport -eq 5000 | select Localport,OwningProcess
Localport OwningProcess
--------- -------------
5000 1684

NoName
- 7,940
- 13
- 56
- 108

mikemaccana
- 110,530
- 99
- 389
- 494
3
If you are using PowerShell Core v6 and above use Test-Connection
>Test-Connection localhost -TcpPort 80
True

Kyle Nunery
- 2,048
- 19
- 23
1
Following the other answers, once you run Get-NetTCPConnection | where Localport -eq 5000 | select Localport,OwningProcess
you can check which process/application is using the port by running Get-Process
Get-Process -Id <OwningProcess>

crisleo94
- 97
- 1
- 9
-
-
2Once you get the OwningProcess with this `Get-NetTCPConnection | where Localport -eq 5000 | select Localport,OwningProcess` you then run `Get-Process -Id
` and you know wich application or process is using the port so you can kill that process from the task manager – crisleo94 Aug 10 '22 at 14:00 -
Yes i mean exactly You need both to get the answer in this better detail, i would recommend updating Your solution to contain both perhaps even using an array variable to store the result and look the other thing up to flex Your script muscles and surely be better and more complete than the other suggestions in themselves to totally justify why yours is a viable and relevant addition to the other suggestion(s) – T. Nielsen Aug 22 '22 at 12:27
0
You could just roll it into one:
Get-NetTCPConnection | where Localport -eq 5000| Select-Object Localport,@{'Name' = 'ProcessName';'Expression'={(Get-Process -Id $_.OwningProcess).Name}}
-
Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 03 '22 at 09:52