test-connection — Ping a host — returns structured pings or quiet boolean across all 5 shells
Equivalents in every shell
ping -c 4 example.comStandard 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.
ping -c 4 example.comSame 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`.
ping -c 4 example.comSame `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.
Test-Connection example.com -Count 4PowerShell-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.
ping -n 4 example.comBuilt-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
ping -c 4 example.comTest-Connection example.com -Count 4ping -n 4 example.comTest TCP port reachability (not ICMP)
nc -zv example.com 443Test-Connection example.com -TcpPort 443powershell -Command "Test-NetConnection example.com -Port 443"Quick existence-check returning $true / $false
ping -c 1 -W 2 example.com >/dev/null 2>&1 && echo upTest-Connection example.com -Count 1 -QuietGotchas
- `-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.