在 Windows 用 Test-NetConnection 测试 TCP 端口连通性

Windows 自带的 PowerShell 一行命令,直接回答“这个网络的 5432/3389/443 端口通不通”——不再需要安装 telnet 客户端。

Windows网络故障排查PowerShell低风险
运行 `Test-NetConnection <主机> -Port <端口>`,查看结果中 TcpTestSucceeded 是否为 True。

01命令

命令
$ Test-NetConnection db-server.example.com -Port 5432

02作用说明

Test-NetConnection 会真正对目标主机和端口做一次 TCP 三次握手,输出 PingSucceeded、TcpTestSucceeded 等诊断信息。ICMP ping 经常能通而实际端口被防火墙拦住,所以测试 RDP、SQL、SMB、SMTP 的可达性要用这个命令才算数。想要脚本可读的布尔结果,可以加 `| Select-Object -ExpandProperty TcpTestSucceeded`。它比 ping 慢,因为要等 TCP 超时——结果长时间挂着通常意味着“被拦截”而不是“服务坏了”。

03风险

风险低风险

04撤销

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

05来源

  • Test-NetConnection - PowerShell
    SS64 · technical · 2026-09-17
    依据: SS64 documents Test-NetConnection's -Port parameter for testing TCP connectivity and the TcpTestSucceeded result property.
  • PING - Windows CMD
    SS64 · technical · 2026-09-17
    依据: SS64 explains ping only tests ICMP echo, which many hosts and firewalls block, unlike a TCP port test.

最后验证: 2026-09-17 · 分类: 网络

06相关方案