PowerShell: A Ping Log That Actually Works

What You Want It For

Intermittent network drops are the worst kind of problem: by the time you look, everything is fine. A log that pings a target on a schedule and appends the result to a file turns "the internet keeps dropping out" into something with timestamps you can show an ISP.

The script is short. The version of it that floats around has two bugs, and both are the kind that look fine until the log is empty or the loop runs forever in a way you did not intend.

A photorealistic shot of a laptop with a terminal window scrolling a list of timestamps and true/false values, an Ethernet cable leading to a router in the background, evening light, realistic photography, no legible text.

The Corrected Script

# PingLog.ps1
$outputPath = Join-Path $env:USERPROFILE 'ping_output.csv'
$target     = 'google.com'
$interval   = 60          # seconds between checks

if (-not (Test-Path $outputPath)) {
    'Timestamp,Target,Success,LatencyMs' | Out-File -FilePath $outputPath -Encoding utf8
}

while ($true) {
    $stamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
    $ok    = Test-Connection -ComputerName $target -Count 1 -Quiet

    $latency = ''
    if ($ok) {
        $rtt = (Test-Connection -ComputerName $target -Count 1 -ErrorAction SilentlyContinue).ResponseTime
        if ($rtt) { $latency = $rtt }
    }

    $line = "$stamp,$target,$ok,$latency"
    Write-Host $line
    $line | Out-File -FilePath $outputPath -Append -Encoding utf8

    Start-Sleep -Seconds $interval
}

Stop it with Ctrl+C.

The Two Bugs In The Original

while ($yes = 'True') is an assignment, not a comparison. In PowerShell, = assigns. The assignment succeeds and returns 'True', which is truthy, so the loop happens to run — but for the wrong reason, and any value you assign that is falsy would silently end the loop. The correct form is while ($true), or while ($condition -eq 'value') if you genuinely want a comparison.

"~\ping_output.txt" does not expand. PowerShell does not resolve ~ inside a string the way a shell does. Out-File will try to create a file literally named ~\ping_output.txt relative to the working directory, which usually means a confusing error or a stray directory. Use $env:USERPROFILE or Join-Path:

$outputPath = Join-Path $env:USERPROFILE 'ping_output.txt'

Making The Log More Useful

Log latency, not just success. A link that never fully fails but climbs from 20ms to 400ms is a real fault, and a boolean log cannot show it. The ResponseTime capture above gives you that.

Log to CSV. With a header row, the output opens in Excel and you can chart it. A plain text log of date, True needs parsing before it tells you anything.

Add the timestamp to the host, not the result. The original wrote "$date, $test" where $date was a [DateTime] object; that stringifies with the current culture and may include characters your CSV tool will misread. Format it explicitly as above.

Rotate or cap it. One line a minute is 525,600 lines a year. At 60-second intervals that is a few megabytes, which is fine; at 5 seconds it is not. Either increase the interval or add a monthly filename:

$logName = "ping_$(Get-Date -Format 'yyyy-MM').csv"

A photorealistic close-up of a spreadsheet showing two columns of timestamps and latency figures with a line chart dropping sharply, a laptop on a kitchen table, daylight, realistic photography, no legible text.

The Caveat That Bites Everybody

ICMP is not the internet. Test-Connection sends an ICMP echo. Plenty of networks and providers deprioritise or outright block ICMP while happily passing TCP. A log full of failures against a host that blocks pings tells you nothing about your connection.

If you are troubleshooting a real service, test the thing you actually use:

# does the actual service answer?
Test-NetConnection -ComputerName your.server -Port 443 -InformationLevel Quiet

For an ISP dispute, ping something that is both close to you and reliably answers ICMP — your ISP's first hop is usually better than a public name, because it isolates their network from the wider internet.

A photorealistic shot of a small home router with blinking status lights on a shelf, an Ethernet cable running to a wall socket, evening room light, realistic photography, no legible text.

Running It In The Background

A visible console window is fine while you are testing. For days, register it as a scheduled task at logon, or start it with:

Start-Process PowerShell -ArgumentList '-NoProfile -File C:\Scripts\PingLog.ps1' -WindowStyle Hidden

Do not run it elevated. It needs no privileges, and a permanent elevated PowerShell process is a bad habit to build for a diagnostic script.

Don't trust the download button. Trust the hash.

If this was useful, More hands-on tech guides at TechGeek. No pressure — read it whenever it's useful.


Luke Keam
Spot a bug or want a custom utility built? I'd love to hear from you. Get in touch
Luke Keam | techgeek.biz