Powershell – ping with a timestamp

I was recently troubleshooting intermittent network connectivity and couldn’t find anything that was free and that would allow me to log a timestamp along with a ping reply. So I have put together few PS lines to do just this – in essence when started you will be asked if you want to log your pings to a file or just display them in console.

$destination = Read-Host "Specify destination host name or IP address "

    If($logonly -notmatch '^[yn]$' ) { Write-Warning "Invalid Entry" }
    $logonly = Read-Host "Log to a file and Output to console? (Y/N) "
} While($logonly -notmatch '^[yn]$')

Write-Host "Default ping vaules: -count 999999 -delay 2"

if ($logonly -eq "y") { 
    write-host "Pinging host:"$destination". Log file location: "$filelocation -BackgroundColor Yellow -ForegroundColor Red
    test-connection $destination -count 999999 -delay 2 -Verbose | format-table @{n='TimeStamp';e={Get-Date}},@{Expression={$_.Address};Label='Destination'},IPV4aDDRESS,ResponseTime | tee-object -filepath $filelocation -Append 

else { 
    test-connection $destination -count 999999 -delay 2 -Verbose | format-table @{n='TimeStamp';e={Get-Date}},@{Expression={$_.Address};Label="Destination"},IPV4aDDRESS,ResponseTime