Is there a way in pow开发者_运维技巧ershell to check if a given port is in use or not?
Use Get-NetTCPConnection
Get-NetTCPConnection | where Localport -eq 5000 | select Localport,OwningProcess
Localport OwningProcess
--------- -------------
5000 1684
If you are using PowerShell Core v6 and above use Test-Connection
>Test-Connection localhost -TcpPort 80
True
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>
You could just roll it into one:
Get-NetTCPConnection | where Localport -eq 5000| Select-Object Localport,@{'Name' = 'ProcessName';'Expression'={(Get-Process -Id $_.OwningProcess).Name}}
精彩评论