Skip to content

Instantly share code, notes, and snippets.

@zulhfreelancer
Created April 23, 2025 13:36
Show Gist options
  • Save zulhfreelancer/2a587cdff3bf2cc7743110e15a2c90d2 to your computer and use it in GitHub Desktop.
Save zulhfreelancer/2a587cdff3bf2cc7743110e15a2c90d2 to your computer and use it in GitHub Desktop.
PowerShell ping time loop

This PowerShell script will continuously prints the current time and pings an IP address.

It will run in a loop until you press Ctrl+C to stop it.

# Replace <IP> with target host IP address
try {
    while ($true) {
        $timestamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
        $pingResult = ping <IP> -n 1

        if ($pingResult -match "Reply from") {
            Write-Host "$timestamp - Host is up"
        } else {
            Write-Host "$timestamp - Host is down"
        }

        Start-Sleep -Seconds 1
    }
} catch {
    Write-Host "Script interrupted."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment