Skip to content
shellmap

test-connectionPing a host — returns structured pings or quiet boolean across all 5 shells

Equivalents in every shell

Bashunix
ping -c 4 example.com

Standard ICMP echo. `-c N` stops after N packets; `-W <sec>` sets timeout. Pure ICMP — many cloud firewalls drop it, so `ping` failing does not mean the host is down. Use `nc -zv host port` or `curl -s -o /dev/null -w "%{http_code}" https://host` to verify TCP/HTTP reachability.

Zshunix
ping -c 4 example.com

Same external `ping` binary as bash. macOS `ping` defaults to continuous (no `-c`); pass `-c 4` for parity with Linux behaviour. For TCP port checks: `nc -zv example.com 443`.

Fishunix
ping -c 4 example.com

Same `ping`. Fish has no built-in TCP-reach test; use `nc -zv host port` or fish's `command` to invoke an installed tool. `curl --connect-timeout 5 -sI https://host` is a quick HTTP-level check.

PowerShellwindows
Test-Connection example.com -Count 4

PowerShell-native cmdlet. Returns `[PingReply]`-like objects with `.ResponseTime`, `.Status`, `.Address`, `.BufferSize`. pwsh 7+ adds `-TcpPort <port>` (skip ICMP, dial TCP) and `-Traceroute` (returns hop-by-hop). On Windows PowerShell 5.1, the closely-related `Test-NetConnection` is the TCP-aware alternative.

cmd.exewindows
ping -n 4 example.com

Built-in. `-n N` sets count (Windows is `-n`, Linux is `-c` — a frequent source of cross-platform script breakage). `-w <ms>` for per-packet timeout. No TCP-test mode — use `powershell -Command "Test-NetConnection ..."` for TCP reachability from inside cmd.

Worked examples

Send 4 pings and stop

Bash
ping -c 4 example.com
PowerShell
Test-Connection example.com -Count 4
cmd.exe
ping -n 4 example.com

Test TCP port reachability (not ICMP)

Bash
nc -zv example.com 443
PowerShell
Test-Connection example.com -TcpPort 443
cmd.exe
powershell -Command "Test-NetConnection example.com -Port 443"

Quick existence-check returning $true / $false

Bash
ping -c 1 -W 2 example.com >/dev/null 2>&1 && echo up
PowerShell
Test-Connection example.com -Count 1 -Quiet

Gotchas

  • `-Quiet` returns a plain `[bool]` (`$true` on any successful packet, `$false` on all-fail) instead of `PingReply` objects. Ideal for scripts that branch on reachability without parsing structured output. Without `-Quiet` you ALWAYS get objects, even when the host is down (with `.Status = TimedOut`).
  • pwsh 7 introduced `-TcpPort` but the older pwsh 5.1 cmdlet (`Test-Connection` on Windows PowerShell) does NOT support it — porting a pwsh 7 script to 5.1 means falling back to `Test-NetConnection -Port`. Detect the version with `$PSVersionTable.PSVersion.Major -ge 7`.
  • ICMP requires elevated privileges on some Linux distros — `Test-Connection` on Linux pwsh shells out to the system `ping`, so capability rules apply: either `CAP_NET_RAW` on the ping binary, or `sysctl -w net.ipv4.ping_group_range="0 2147483647"` to allow unprivileged users.
  • `-Count 0` does NOT mean unlimited — it means zero packets and immediate exit. Use `-Continuous` (pwsh 7+) to ping forever like Unix `ping` with no `-c`. On Windows PowerShell 5.1 there is no `-Continuous` switch; loop `while($true) { Test-Connection … }` instead.
  • Default timeout is 5 seconds per packet — slow when the target is unreachable. Pass `-TimeoutSeconds 1` (pwsh 7+) for fast-fail behaviour. On pwsh 5.1, the cmdlet does not expose a timeout knob; use `Test-NetConnection -InformationLevel Quiet` which fails faster.

WSL & PowerShell Core notes

pwsh`Test-Connection` is fully cross-platform on pwsh 7+ (Windows, Linux, macOS) and is the recommended portable form. On Linux/macOS pwsh it shells out to the system `ping` binary under the hood — so all platform-native ICMP rules apply, including `CAP_NET_RAW` / `setuid` requirements on Linux. `-TcpPort` (pwsh 7+) uses .NET's `TcpClient` directly and is identical on every platform.
WSLInside WSL bash, `ping example.com` works (NAT routes outbound), but `ping <windows-host-ip>` only succeeds if Windows Firewall has the inbound ICMP echo rule enabled, which is OFF by default on private networks. To reach `Test-Connection` from WSL, install pwsh inside the distro or invoke via interop: `pwsh.exe -c "Test-Connection example.com -Count 4"`. Conversely, from Windows-side pwsh, `Test-Connection $(wsl hostname -I)` checks the WSL VM's dynamic IP.

Related commands