← Back to Blog

PowerShell script to diagnose packet loss issues. This script will:

PowerShell script to diagnose packet loss issues. This script will: Ping a target and calculate packet loss percentage. Perform a traceroute to identify potential problematic hops.

PowerShell script to diagnose packet loss issues. This script will:

  1. Ping a target and calculate packet loss percentage.

  2. Perform a traceroute to identify potential problematic hops.

  3. Log the results for analysis.

PowerShell Script:

Define variables

$TargetHost = "8.8.8.8" # Replace with the destination IP or hostname

$PingCount = 20 # Number of pings to send

$LogFilePath = "C:\NetworkDiagnostics\PacketLossLog.txt"

Create log directory if it doesn't exist

if (!(Test-Path -Path (Split-Path -Path $LogFilePath))) {

New-Item -ItemType Directory -Path (Split-Path -Path $LogFilePath)
                

}

Start logging

$StartTime = Get-Date

"===== Packet Loss Diagnostics =====" | Out-File -Append -FilePath $LogFilePath

"Start Time: $StartTime" | Out-File -Append -FilePath $LogFilePath

"Target Host: $TargetHost" | Out-File -Append -FilePath $LogFilePath

"Ping Count: $PingCount" | Out-File -Append -FilePath $LogFilePath

"===================================" | Out-File -Append -FilePath $LogFilePath

Step 1: Ping test

Write-Host "Performing ping test to $TargetHost..."

$PingResults = Test-Connection -ComputerName $TargetHost -Count $PingCount -ErrorAction SilentlyContinue

if ($PingResults) {

$Sent = $PingResults.Count
                
                $Received = ($PingResults | Measure-Object -Property ResponseTime -GreaterThan 0).Count
                
                $Lost = $Sent - $Received
                
                $LossPercentage = [math]::Round(($Lost / $Sent) * 100, 2)
                
                # Log ping results
                
                "Ping Test Results:" | Out-File -Append -FilePath $LogFilePath
                
                "  Packets Sent: $Sent" | Out-File -Append -FilePath $LogFilePath
                
                "  Packets Received: $Received" | Out-File -Append -FilePath $LogFilePath
                
                "  Packets Lost: $Lost" | Out-File -Append -FilePath $LogFilePath
                
                "  Packet Loss: $LossPercentage%" | Out-File -Append -FilePath $LogFilePath
                
                "===================================" | Out-File -Append -FilePath $LogFilePath
                
                Write-Host "Ping test complete. Packet loss: $LossPercentage%"
                

} else {

"Ping test failed. Unable to reach $TargetHost." | Out-File -Append -FilePath $LogFilePath
                
                Write-Host "Ping test failed. Unable to reach $TargetHost."
                

}

Step 2: Traceroute

Write-Host "Performing traceroute to $TargetHost..."

"Traceroute Results:" | Out-File -Append -FilePath $LogFilePath

$TracerouteResults = tracert $TargetHost | ForEach-Object { $_ -replace " ", "" }

if ($TracerouteResults) {

$TracerouteResults | Out-File -Append -FilePath $LogFilePath
                
                Write-Host "Traceroute completed. Results logged to $LogFilePath."
                

} else {

"Traceroute failed or returned no results." | Out-File -Append -FilePath $LogFilePath
                
                Write-Host "Traceroute failed or returned no results."
                

}

Step 3: Log end time

$EndTime = Get-Date

"End Time: $EndTime" | Out-File -Append -FilePath $LogFilePath

"===================================" | Out-File -Append -FilePath $LogFilePath

Step 4: Notify user

Write-Host "Diagnostics completed. Results saved to $LogFilePath."

Script Breakdown:

  1. Ping Test:

• Uses Test-Connection to ping the target host multiple times.

• Calculates sent, received, and lost packets to determine packet loss percentage.

• Logs the results to a file.

  1. Traceroute:

• Uses the tracert command to identify problematic hops along the route.

• Logs the results to the same log file.

  1. Error Handling:

• Logs an error message if the ping or traceroute fails.

  1. Logging:

• Saves all diagnostic data to a log file for later analysis.

  1. Customization:

• Modify $TargetHost, $PingCount, and $LogFilePath as needed.

How to Run the Script:

  1. Open PowerShell as an administrator.

  2. Copy the script into a .ps1 file (e.g., PacketLossDiagnostics.ps1).

  3. Run the script:

.\PacketLossDiagnostics.ps1

  1. Review the log file (e.g., C:\NetworkDiagnostics\PacketLossLog.txt) for detailed results.

This script provides a solid starting point for diagnosing packet loss and logging the findings systematically.


Imported from rifaterdemsahin.com · 2025