← All scripts
POWERSHELL Version 1.0.0

Internet speed test

Check your internet speed, using the Ookla CLI.

Source code

SHA-256: aa0cf7a70a3d87205fdfa2b5b5be0422f1c30b600658c4d5b631991f434e9d98

# ScriptSnatch.com
# Internet Speed Test
# Automatically installs Ookla Speedtest CLI if missing

$ErrorActionPreference = "Stop"

function Find-Speedtest {

    $cmd = Get-Command speedtest.exe -ErrorAction SilentlyContinue

    if ($cmd) {
        return $cmd.Source
    }

    # Check common WinGet installation locations
    $folders = @(
        "$env:LOCALAPPDATA\Microsoft\WinGet\Links",
        "$env:ProgramFiles\WinGet\Links"
    )

    foreach ($folder in $folders) {
        $path = Join-Path $folder "speedtest.exe"

        if (Test-Path $path) {
            return $path
        }
    }

    return $null
}

Write-Host ""
Write-Host "=====================================" -ForegroundColor Cyan
Write-Host "       INTERNET SPEED TEST" -ForegroundColor Cyan
Write-Host "=====================================" -ForegroundColor Cyan
Write-Host ""

try {

    # Check if Speedtest CLI is already installed
    $speedtest = Find-Speedtest

    if (-not $speedtest) {

        Write-Host "Ookla Speedtest CLI not found." -ForegroundColor Yellow
        Write-Host "Installing automatically..." -ForegroundColor Cyan
        Write-Host ""

        # Check WinGet availability
        $winget = Get-Command winget.exe -ErrorAction SilentlyContinue

        if (-not $winget) {
            throw "WinGet is not installed. Install App Installer from Microsoft first."
        }

        # Install Ookla Speedtest CLI
        & $winget.Source install `
            --id Ookla.Speedtest.CLI `
            --exact `
            --accept-source-agreements `
            --accept-package-agreements `
            --silent

        if ($LASTEXITCODE -ne 0) {
            throw "Installation failed. WinGet exit code: $LASTEXITCODE"
        }

        # Find executable after installation
        $speedtest = Find-Speedtest

        if (-not $speedtest) {
            throw "Installation completed, but speedtest.exe could not be found. Open a new terminal and try again."
        }

        Write-Host ""
        Write-Host "Ookla Speedtest CLI installed successfully!" -ForegroundColor Green
    }
    else {
        Write-Host "Ookla Speedtest CLI is already installed." -ForegroundColor Green
    }

    Write-Host ""
    Write-Host "Testing your internet connection..." -ForegroundColor Yellow
    Write-Host ""

    # Run Speedtest CLI
    $json = & $speedtest `
        --accept-license `
        --accept-gdpr `
        --format=json

    if ($LASTEXITCODE -ne 0) {
        throw "Speedtest failed. Exit code: $LASTEXITCODE"
    }

    $result = $json | ConvertFrom-Json

    # Convert bytes/sec to Mbps
    $download = [math]::Round(
        ($result.download.bandwidth * 8) / 1000000, 2
    )

    $upload = [math]::Round(
        ($result.upload.bandwidth * 8) / 1000000, 2
    )

    $ping = [math]::Round($result.ping.latency, 2)

    Write-Host "=====================================" -ForegroundColor Green
    Write-Host "             RESULTS" -ForegroundColor Green
    Write-Host "=====================================" -ForegroundColor Green
    Write-Host ""

    Write-Host "Download : $download Mbps" -ForegroundColor Green
    Write-Host "Upload   : $upload Mbps" -ForegroundColor Green
    Write-Host "Ping     : $ping ms" -ForegroundColor Yellow

    Write-Host ""
    Write-Host "ISP      : $($result.isp)"
    Write-Host "Server   : $($result.server.name)"
    Write-Host "Location : $($result.server.location)"
    Write-Host ""

    if ($result.result.url) {
        Write-Host "Result URL: $($result.result.url)" -ForegroundColor Cyan
    }
}
catch {
    Write-Host ""
    Write-Host "ERROR: $($_.Exception.Message)" -ForegroundColor Red
}
Download file

Community reviews

No published reviews yet.

Leave a review

Reviews are moderated. Your email is not displayed publicly.