结束占用端口的程序(Windows)
用 Get-NetTCPConnection 找到占用 TCP 端口的进程,再用 Stop-Process 结束它。
先运行 `Get-NetTCPConnection -LocalPort 8080 | Select-Object LocalPort, State, OwningProcess` 拿到 PID,再运行 `Stop-Process -Id <pid>` 结束该进程。
01命令
命令
$ Get-NetTCPConnection -LocalPort 8080 | Select-Object LocalPort, State, OwningProcess :: then: Stop-Process -Id (Get-NetTCPConnection -LocalPort 8080).OwningProcess02作用说明
当开发服务器或旧程序不肯释放 8080(或其他)端口时,Get-NetTCPConnection 会列出该端口上的所有连接,其中 OwningProcess 列就是占用它的进程 PID。用 Stop-Process 直接结束该 PID 即可释放端口;对不肯正常退出的进程可加 -Force。
03风险
!注意: 结束前先看清是哪个进程——误杀系统或数据库服务可能导致依赖它的其他程序出问题。
04撤销
✓无需撤销——该命令不会改变持久状态。
05来源
- Get-NetTCPConnection依据: Documents -LocalPort as a filter parameter and the OwningProcess property of returned NetTCPConnection objects (the PID of the process that owns the connection).
- Stop-Process — Stop a running process依据: Syntax shows Stop-Process -id <Int32[]> (and -Name/-Force) for terminating processes by process id.
最后验证: 2026-09-19 · 分类: 系统与设置