PowerShell Genie

Measure endpoint latency

Script 2 min read PowerShell 7 only

Documentation: Microsoft Learn

The loop sends four ICMP probes per target with Test-Connection and pipes the replies into Measure-Object, which computes the average, minimum, and maximum response time.

The script

1PS> $Targets = "SRV01", "SRV02"
2PS> foreach($Target in $Targets){ Test-Connection $Target -Count 4 | Measure-Object Latency -Average -Minimum -Maximum | Select-Object @{ Name = "Target"; Expression = { $Target } },Average,Minimum,Maximum }

Script reader

Inspect before you run

Inspection only

Inspect the script below. It does not run in this browser.

Inspection only — no command has run.

    Show reference output
    1 Target
    2 ------
    3 SRV01
    Version compatibility

    PowerShell 7 only. This example measures the Latency property returned by PowerShell 7's Test-Connection. On Windows PowerShell 5.1 the equivalent property is ResponseTime — on 5.1 this script fails instead: Test-Connection returns Win32_PingStatus objects, which have no Latency property, so Measure-Object Latency reports the error Property "Latency" cannot be found in any object(s) input. Swap Latency for ResponseTime to run it on 5.1.

    Inspect this example’s structure

    Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.

    $Targets = "SRV01", "SRV02" foreach($Target in $Targets){ Test-Connection $Target -Count 4 | Measure-Object Latency -Average -Minimum -Maximum | Select-Object @{ Name = "Target"; Expression = { $Target } },Average,Minimum,Maximum }
    1. Stage 1$TargetsPositional values or no named parameters

      How it works

      The loop sends four ICMP probes per target with Test-Connection and pipes the replies into Measure-Object, which computes the average, minimum, and maximum response time. A calculated property stamps the target name onto each summary row so the results stay attributable.

      Adapting it

      Raise -Count for a longer sample when the problem comes and goes, and add -Delay to spread the probes out rather than firing them back to back into the same queue.

      Requirements

      ICMP permitted to the targets, which plenty of hardened environments block outright.

      If the result is unexpected

      A latency report shows failures or inconsistent times despite a working application.

      ICMP may be blocked or deprioritized, and a small sample does not represent application response time.

      1. Separate ping loss and round-trip time from a test of the actual application endpoint.

      Validate the result

      The report states protocol, sample count and failures instead of averaging failures into a valid time.

      Keep the check reliable

      Use a repeatable measurement window and compare like-for-like network paths.

      Primary documentation

      PowerShell objects and pipelines documentation

      Continue
      Genie guideMeasure endpoint latency

      The loop sends four ICMP probes per target with Test-Connection and pipes the replies into Measure-Object, which computes the average, minimum, and maximum response time.