SEO Checker Tool

<#
.SYNOPSIS
SEO Checker Tool

.DESCRIPTION
This script analyzes a webpage for various SEO elements and provides a report.

.NOTES
File Name      : SEOChecker.ps1
Author         : [Your Name]
Prerequisite   : PowerShell V5.1 or later, Internet connectivity
Version        : 1.0
Date           : [Current Date]

.EXAMPLE
.\SEOChecker.ps1
#>

# Import required modules
Add-Type -AssemblyName System.Web

# Global variables
$script:reportPath = "$env:USERPROFILE\Desktop\SEO_Analysis_Report_$(Get-Date -Format 'yyyyMMdd_HHmmss').html"

function Show-Menu {
    Clear-Host
    Write-Host "=== SEO Checker Tool ===" -ForegroundColor Cyan
    Write-Host "1. Analyze Website"
    Write-Host "2. View Last Report"
    Write-Host "3. Exit"
}

function Analyze-Website {
    $url = Read-Host "Enter the website URL to analyze (include http:// or https://)"
    
    if (-not ($url -match "^https?://")) {
        Write-Host "Invalid URL. Please include http:// or https://" -ForegroundColor Red
        return
    }

    Write-Host "`nAnalyzing $url..." -ForegroundColor Yellow

    try {
        $response = Invoke-WebRequest -Uri $url -UseBasicParsing
        $content = $response.Content
        $statusCode = $response.StatusCode

        $results = @{
            URL = $url
            StatusCode = $statusCode
            Title = Get-Title $content
            MetaDescription = Get-MetaDescription $content
            HeaderTags = Get-HeaderTags $content
            ImageAltTags = Get-ImageAltTags $content
            WordCount = Get-WordCount $content
            LoadTime = Measure-LoadTime $url
            MobileFriendly = Test-MobileFriendly $url
            SSL = Test-SSL $url
        }

        Generate-Report $results
    }
    catch {
        Write-Host "Error analyzing website: $_" -ForegroundColor Red
    }
}

function Get-Title($content) {
    if ($content -match "<title>(.*?)</title>") {
        return $matches[1]
    }
    return "No title found"
}

function Get-MetaDescription($content) {
    if ($content -match '<meta name="description" content="(.*?)"') {
        return $matches[1]
    }
    return "No meta description found"
}

function Get-HeaderTags($content) {
    $headers = @{}
    for ($i = 1; $i -le 6; $i++) {
        $count = ([regex]::Matches($content, "<h$i")).Count
        if ($count -gt 0) {
            $headers["H$i"] = $count
        }
    }
    return $headers
}

function Get-ImageAltTags($content) {
    $images = [regex]::Matches($content, '<img [^>]*alt="([^"]*)"')
    return @{
        TotalImages = $images.Count
        ImagesWithAlt = ($images | Where-Object { $_.Groups[1].Value -ne "" }).Count
    }
}

function Get-WordCount($content) {
    $text = $content -replace '<[^>]+>', '' -replace '&nbsp;', ' '
    $words = $text -split '\s+' | Where-Object { $_ -ne '' }
    return $words.Count
}

function Measure-LoadTime($url) {
    $stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
    Invoke-WebRequest -Uri $url -UseBasicParsing | Out-Null
    $stopwatch.Stop()
    return [math]::Round($stopwatch.Elapsed.TotalSeconds, 2)
}

function Test-MobileFriendly($url) {
    # This is a basic check. For a more accurate test, you'd need to use Google's Mobile-Friendly Test API
    $userAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 12_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Mobile/15E148 Safari/604.1"
    $response = Invoke-WebRequest -Uri $url -UserAgent $userAgent -UseBasicParsing
    return $response.StatusCode -eq 200
}

function Test-SSL($url) {
    return $url.StartsWith("https://")
}

function Generate-Report($results) {
    $reportContent = @"
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SEO Analysis Report</title>
    <style>
        body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; max-width: 800px; margin: 0 auto; padding: 20px; }
        h1, h2 { color: #0078D4; }
        table { border-collapse: collapse; width: 100%; margin-bottom: 20px; }
        th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
        th { background-color: #f2f2f2; }
        .good { color: green; }
        .warning { color: orange; }
        .bad { color: red; }
    </style>
</head>
<body>
    <h1>SEO Analysis Report</h1>
    <p>Generated on: $(Get-Date)</p>
    <p>URL: $($results.URL)</p>

    <h2>General Information</h2>
    <table>
        <tr><th>Status Code</th><td>$($results.StatusCode)</td></tr>
        <tr><th>Title</th><td>$($results.Title)</td></tr>
        <tr><th>Meta Description</th><td>$($results.MetaDescription)</td></tr>
        <tr><th>Word Count</th><td>$($results.WordCount)</td></tr>
        <tr><th>Load Time</th><td>$($results.LoadTime) seconds</td></tr>
        <tr><th>Mobile Friendly</th><td>$(if ($results.MobileFriendly) { "Yes" } else { "No" })</td></tr>
        <tr><th>SSL</th><td>$(if ($results.SSL) { "Yes" } else { "No" })</td></tr>
    </table>

    <h2>Header Tags</h2>
    <table>
        $(foreach ($header in $results.HeaderTags.GetEnumerator()) {
            "<tr><th>$($header.Key)</th><td>$($header.Value)</td></tr>"
        })
    </table>

    <h2>Images</h2>
    <table>
        <tr><th>Total Images</th><td>$($results.ImageAltTags.TotalImages)</td></tr>
        <tr><th>Images with Alt Text</th><td>$($results.ImageAltTags.ImagesWithAlt)</td></tr>
    </table>

    <h2>Recommendations</h2>
    <ul>
        $(if ($results.Title.Length -gt 60) { "<li class='warning'>Title is too long ($(($results.Title).Length) characters). Keep it under 60 characters.</li>" })
        $(if ($results.Title.Length -lt 30) { "<li class='warning'>Title is too short ($(($results.Title).Length) characters). Aim for 30-60 characters.</li>" })
        $(if ($results.MetaDescription.Length -gt 160) { "<li class='warning'>Meta description is too long ($(($results.MetaDescription).Length) characters). Keep it under 160 characters.</li>" })
        $(if ($results.MetaDescription.Length -lt 50) { "<li class='warning'>Meta description is too short ($(($results.MetaDescription).Length) characters). Aim for 50-160 characters.</li>" })
        $(if ($results.WordCount -lt 300) { "<li class='warning'>Content might be too short ($($results.WordCount) words). Aim for at least 300 words.</li>" })
        $(if ($results.LoadTime -gt 3) { "<li class='warning'>Page load time is slow ($($results.LoadTime) seconds). Aim for under 3 seconds.</li>" })
        $(if (-not $results.MobileFriendly) { "<li class='bad'>Page may not be mobile-friendly. Consider optimizing for mobile devices.</li>" })
        $(if (-not $results.SSL) { "<li class='bad'>Site is not using SSL. Consider switching to HTTPS for better security and SEO.</li>" })
        $(if ($results.ImageAltTags.ImagesWithAlt -lt $results.ImageAltTags.TotalImages) { "<li class='warning'>Not all images have alt text. Add alt text to improve accessibility and SEO.</li>" })
    </ul>
</body>
</html>
"@

    $reportContent | Out-File -FilePath $script:reportPath
    Write-Host "Report generated and saved to: $script:reportPath" -ForegroundColor Green
}

function View-LastReport {
    if (Test-Path $script:reportPath) {
        Start-Process $script:reportPath
    } else {
        Write-Host "No report found. Please analyze a website first." -ForegroundColor Yellow
    }
}

# Main program loop
do {
    Show-Menu
    $choice = Read-Host "`nEnter your choice (1-3)"

    switch ($choice) {
        "1" { Analyze-Website }
        "2" { View-LastReport }
        "3" { Write-Host "Exiting program..." -ForegroundColor Yellow; break }
        default { Write-Host "Invalid choice. Please try again." -ForegroundColor Red }
    }

    if ($choice -ne "3") {
        Read-Host "`nPress Enter to continue..."
    }
} while ($choice -ne "3")

This SEO Checker Tool includes:

  1. A menu-driven interface for easy navigation.
  2. Functions to analyze various SEO elements of a webpage:
    • Page title
    • Meta description
    • Header tags (H1 to H6)
    • Image alt tags
    • Word count
    • Page load time
    • Mobile-friendliness (basic check)
    • SSL implementation
  3. A report generation function that creates an HTML report with the analysis results and recommendations.
  4. Option to view the last generated report.

Key features:

  • Analyzes important on-page SEO elements
  • Provides a visual HTML report with color-coded recommendations
  • Checks for mobile-friendliness and SSL implementation
  • Measures page load time
  • Offers suggestions for improvement based on common SEO best practices

This tool is particularly useful for:

  • Website owners who want to perform a basic SEO check
  • Digital marketers analyzing website SEO elements
  • Web developers ensuring they’ve implemented basic SEO best practices
  • Anyone learning about SEO and wanting to analyze real websites

To use this script effectively:

  1. Run the script in PowerShell
  2. Choose to analyze a website by entering its URL
  3. Review the generated HTML report
  4. Use the recommendations to improve the website’s SEO

Please note that this is a basic SEO checker and doesn’t cover all aspects of SEO. For a comprehensive SEO analysis, you would need to consider many more factors and potentially use more advanced APIs and tools. However, this script provides a good starting point for basic on-page SEO analysis.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *