结束占用端口的程序(Windows)

用 Get-NetTCPConnection 找到占用 TCP 端口的进程,再用 Stop-Process 结束它。

Windows进程网络系统PowerShell中风险
先运行 `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).OwningProcess

02作用说明

当开发服务器或旧程序不肯释放 8080(或其他)端口时,Get-NetTCPConnection 会列出该端口上的所有连接,其中 OwningProcess 列就是占用它的进程 PID。用 Stop-Process 直接结束该 PID 即可释放端口;对不肯正常退出的进程可加 -Force。

03风险

风险中风险
使用条件Use an elevated (Administrator) PowerShell to inspect or stop processes you don't own.
!注意: 结束前先看清是哪个进程——误杀系统或数据库服务可能导致依赖它的其他程序出问题。

04撤销

✓无需撤销——该命令不会改变持久状态。

05来源

  • Get-NetTCPConnection
    Microsoft Learn · vendor-doc · 2026-09-17
    依据: 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
    SS64 · technical · 2026-09-17
    依据: Syntax shows Stop-Process -id <Int32[]> (and -Name/-Force) for terminating processes by process id.

最后验证: 2026-09-19 · 分类: 系统与设置

06相关方案