欢迎光临千百叶网
详情描述

方案一:批处理主控 + PowerShell核心清理

1. ChromeCleaner.bat(批处理主文件)

@echo off
chcp 65001 >nul
title Chrome浏览器缓存清理工具
color 0A

:MAIN_MENU
cls
echo ============================================
echo        Chrome浏览器缓存清理工具
echo ============================================
echo.
echo  请选择操作:
echo.
echo  [1] 清理Chrome缓存
echo  [2] 清理Chrome所有数据
echo  [3] 查看Chrome缓存大小
echo  [4] 打开Chrome数据目录
echo  [5] 仅清理临时文件
echo  [6] 退出
echo.
echo ============================================

set /p choice="请输入选择 (1-6): "

if "%choice%"=="1" goto CLEAN_CACHE
if "%choice%"=="2" goto CLEAN_ALL
if "%choice%"=="3" goto SHOW_SIZE
if "%choice%"=="4" goto OPEN_DIR
if "%choice%"=="5" goto CLEAN_TEMP
if "%choice%"=="6" exit

echo 无效的选择!
pause
goto MAIN_MENU

:CLEAN_CACHE
powershell.exe -ExecutionPolicy Bypass -File "%~dp0ChromeCleaner.ps1" -CleanCache
pause
goto MAIN_MENU

:CLEAN_ALL
echo 警告:此操作将删除Chrome所有数据!
echo 包括书签、密码、扩展等(可保留部分数据)
set /p confirm="确认继续吗?(Y/N): "
if /i "%confirm%"=="Y" (
    powershell.exe -ExecutionPolicy Bypass -File "%~dp0ChromeCleaner.ps1" -CleanAll
)
pause
goto MAIN_MENU

:SHOW_SIZE
powershell.exe -ExecutionPolicy Bypass -File "%~dp0ChromeCleaner.ps1" -ShowSize
pause
goto MAIN_MENU

:OPEN_DIR
powershell.exe -ExecutionPolicy Bypass -File "%~dp0ChromeCleaner.ps1" -OpenDir
pause
goto MAIN_MENU

:CLEAN_TEMP
powershell.exe -ExecutionPolicy Bypass -File "%~dp0ChromeCleaner.ps1" -CleanTemp
pause
goto MAIN_MENU

2. ChromeCleaner.ps1(PowerShell核心清理脚本)

# Chrome浏览器缓存清理工具 - PowerShell版
# 支持多种清理选项和自定义配置

param(
    [Parameter(Mandatory=$false)]
    [ValidateSet("CleanCache", "CleanAll", "ShowSize", "OpenDir", "CleanTemp")]
    [string]$Action = "ShowSize",

    [switch]$Force,
    [switch]$Verbose
)

# 配置参数
$Config = @{
    ChromePaths = @(
        "$env:LOCALAPPDATA\Google\Chrome",
        "$env:APPDATA\Google\Chrome"
    )

    # 要清理的缓存目录
    CacheDirectories = @(
        "User Data\Default\Cache",
        "User Data\Default\Cache2",          # Chrome 85+
        "User Data\Default\Code Cache",
        "User Data\Default\GPUCache",
        "User Data\Default\Service Worker\CacheStorage",
        "User Data\Default\Service Worker\ScriptCache"
    )

    # 要清理的数据目录(谨慎使用)
    DataDirectories = @(
        "User Data\Default\Cookies",
        "User Data\Default\History",
        "User Data\Default\Local Storage",
        "User Data\Default\Session Storage",
        "User Data\Default\Web Data",
        "User Data\Default\Visited Links",
        "User Data\Default\Top Sites",
        "User Data\Default\Favicons"
    )

    # 要保留的重要数据(CleanAll时保留)
    PreserveDirectories = @(
        "User Data\Default\Bookmarks",
        "User Data\Default\Login Data",
        "User Data\Default\Preferences",
        "User Data\Default\Extensions"
    )

    # 临时文件扩展名
    TempExtensions = @("*.tmp", "*.temp", "*.crx", "*.crdownload")
}

# 辅助函数:显示彩色输出
function Write-Colored {
    param([string]$Text, [ConsoleColor]$Color = "White")
    Write-Host $Text -ForegroundColor $Color
}

# 检查Chrome是否在运行
function Test-ChromeRunning {
    $processes = Get-Process -Name "chrome" -ErrorAction SilentlyContinue
    if ($processes) {
        Write-Colored "警告:检测到Chrome正在运行!" -Color Yellow
        Write-Host "建议关闭Chrome后再执行清理操作。"

        if (-not $Force) {
            $choice = Read-Host "是否强制继续?(Y/N)"
            if ($choice -notmatch "^[Yy]$") {
                exit 1
            }
        }

        # 尝试关闭Chrome
        Write-Host "正在关闭Chrome进程..."
        $processes | Stop-Process -Force -ErrorAction SilentlyContinue
        Start-Sleep -Seconds 2
    }
}

# 获取文件夹大小
function Get-FolderSize {
    param([string]$Path)
    if (Test-Path $Path) {
        $size = (Get-ChildItem $Path -Recurse -File | Measure-Object -Property Length -Sum).Sum
        return [math]::Round($size / 1MB, 2)
    }
    return 0
}

# 显示Chrome缓存大小
function Show-CacheSize {
    Write-Colored "`n正在分析Chrome缓存大小..." -Color Cyan

    $totalSize = 0
    foreach ($chromePath in $Config.ChromePaths) {
        if (Test-Path $chromePath) {
            Write-Host "`nChrome目录: $chromePath"
            Write-Host ("-" * 50)

            foreach ($cacheDir in $Config.CacheDirectories) {
                $fullPath = Join-Path $chromePath $cacheDir
                $size = Get-FolderSize $fullPath
                if ($size -gt 0) {
                    Write-Host ("{0,-50} {1,10} MB" -f $cacheDir, $size)
                    $totalSize += $size
                }
            }
        }
    }

    Write-Colored ("`n总缓存大小: {0} MB" -f $totalSize) -Color Green
}

# 清理缓存目录
function Clear-Cache {
    Test-ChromeRunning

    Write-Colored "`n开始清理Chrome缓存..." -Color Cyan

    $cleanedSize = 0
    foreach ($chromePath in $Config.ChromePaths) {
        if (Test-Path $chromePath) {
            foreach ($cacheDir in $Config.CacheDirectories) {
                $fullPath = Join-Path $chromePath $cacheDir
                if (Test-Path $fullPath) {
                    $sizeBefore = Get-FolderSize $fullPath
                    try {
                        Remove-Item $fullPath -Recurse -Force -ErrorAction Stop
                        Write-Host "已清理: $cacheDir"
                        $cleanedSize += $sizeBefore
                    }
                    catch {
                        Write-Host "清理失败: $($_.Exception.Message)" -ForegroundColor Red
                    }
                }
            }
        }
    }

    Write-Colored ("`n清理完成!释放空间: {0} MB" -f $cleanedSize) -Color Green
}

# 清理临时文件
function Clear-TempFiles {
    Write-Colored "`n清理Chrome临时文件..." -Color Cyan

    $tempCleaned = 0
    foreach ($chromePath in $Config.ChromePaths) {
        if (Test-Path $chromePath) {
            foreach ($ext in $Config.TempExtensions) {
                $files = Get-ChildItem -Path $chromePath -Filter $ext -Recurse -File -ErrorAction SilentlyContinue
                foreach ($file in $files) {
                    try {
                        $size = [math]::Round($file.Length / 1MB, 2)
                        Remove-Item $file.FullName -Force
                        $tempCleaned += $size
                        if ($Verbose) {
                            Write-Host "删除: $($file.Name)"
                        }
                    }
                    catch {
                        # 忽略删除错误
                    }
                }
            }
        }
    }

    # 清理系统临时目录中的Chrome文件
    $systemTemp = "$env:TEMP"
    $chromeTempFiles = Get-ChildItem -Path $systemTemp -Filter "chrome*" -Recurse -File -ErrorAction SilentlyContinue
    foreach ($file in $chromeTempFiles) {
        try {
            $size = [math]::Round($file.Length / 1MB, 2)
            Remove-Item $file.FullName -Force
            $tempCleaned += $size
        }
        catch {
            # 忽略删除错误
        }
    }

    Write-Colored ("清理临时文件完成!释放空间: {0} MB" -f $tempCleaned) -Color Green
}

# 清理所有数据(保留重要数据)
function Clear-AllData {
    Test-ChromeRunning

    Write-Colored "警告:此操作将删除Chrome大部分数据!" -Color Red
    Write-Host "将保留书签、密码、扩展等数据。"

    if (-not $Force) {
        $choice = Read-Host "`n确认继续吗?(Y/N)"
        if ($choice -notmatch "^[Yy]$") {
            exit 0
        }
    }

    Write-Colored "`n开始清理Chrome数据..." -Color Cyan

    $totalCleaned = 0
    foreach ($chromePath in $Config.ChromePaths) {
        if (Test-Path $chromePath) {
            # 清理缓存目录
            foreach ($cacheDir in $Config.CacheDirectories) {
                $fullPath = Join-Path $chromePath $cacheDir
                if (Test-Path $fullPath) {
                    $size = Get-FolderSize $fullPath
                    Remove-Item $fullPath -Recurse -Force -ErrorAction SilentlyContinue
                    $totalCleaned += $size
                }
            }

            # 清理数据目录
            foreach ($dataDir in $Config.DataDirectories) {
                $fullPath = Join-Path $chromePath $dataDir
                if (Test-Path $fullPath) {
                    $size = Get-FolderSize $fullPath
                    Remove-Item $fullPath -Recurse -Force -ErrorAction SilentlyContinue
                    $totalCleaned += $size
                }
            }
        }
    }

    # 清理DNS缓存和系统缓存
    Clear-TempFiles

    Write-Colored ("`n数据清理完成!释放空间: {0} MB" -f $totalCleaned) -Color Green
    Write-Host "`n提示:建议重启Chrome浏览器以确保完全生效。"
}

# 打开Chrome数据目录
function Open-ChromeDir {
    $mainPath = "$env:LOCALAPPDATA\Google\Chrome\User Data\Default"
    if (Test-Path $mainPath) {
        Write-Colored "正在打开Chrome数据目录..." -Color Cyan
        explorer.exe $mainPath
    }
    else {
        Write-Colored "未找到Chrome数据目录!" -Color Red
    }
}

# 主执行逻辑
Write-Colored "`n" ("=" * 60) -Color Blue
Write-Colored "   Chrome浏览器缓存清理工具 PowerShell版" -Color Cyan
Write-Colored "   " ("=" * 60) -Color Blue

switch ($Action) {
    "CleanCache" { Clear-Cache }
    "CleanAll"   { Clear-AllData }
    "ShowSize"   { Show-CacheSize }
    "OpenDir"    { Open-ChromeDir }
    "CleanTemp"  { Clear-TempFiles }
    default      { Show-CacheSize }
}

Write-Colored "`n操作完成!" -Color Green

方案二:简化一键清理脚本

QuickClean.bat(一键清理)

@echo off
title Chrome浏览器一键清理工具
echo 正在关闭Chrome浏览器...
taskkill /F /IM chrome.exe >nul 2>&1
taskkill /F /IM chromedriver.exe >nul 2>&1

echo.
echo 正在清理Chrome缓存...
powershell -Command "Get-ChildItem -Path \"$env:LOCALAPPDATA\Google\Chrome\User Data\Default\" -Filter \"Cache*\" -Directory | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue"

echo 正在清理Chrome临时文件...
powershell -Command "Remove-Item -Path \"$env:TEMP\chrome*\" -Recurse -Force -ErrorAction SilentlyContinue"
powershell -Command "Remove-Item -Path \"$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Code Cache\" -Recurse -Force -ErrorAction SilentlyContinue"

echo.
echo 清理完成!
echo.
pause

使用说明

安装使用:

将两个脚本(.bat和.ps1)放在同一目录下 双击运行 ChromeCleaner.bat

功能特点:

多级菜单:批处理提供友好菜单界面 安全保护:清理前检查Chrome运行状态 选择性清理
  • 仅清理缓存(不影响个人数据)
  • 深度清理(删除更多数据)
  • 保留重要数据(书签、密码等)
预览功能:先查看缓存大小再决定清理 详细日志:显示清理的文件和释放空间

高级使用:

计划任务自动清理

# 创建计划任务(每周日凌晨3点清理)
schtasks /create /tn "ChromeWeeklyClean" /tr "C:\路径\QuickClean.bat" /sc weekly /d SUN /st 03:00

命令行参数


# 静默清理缓存
powershell.exe -ExecutionPolicy Bypass -File ChromeCleaner.ps1 -Action CleanCache -Force
仅查看大小

powershell.exe -ExecutionPolicy Bypass -File ChromeCleaner.ps1 -Action ShowSize



## 注意事项
1. 清理前建议关闭Chrome浏览器
2. 清理"所有数据"选项会删除历史记录、cookies等
3. 书签、密码、扩展等关键数据默认会保留
4. 建议定期清理以提升浏览器性能
5. 脚本需要管理员权限来访问某些系统目录

这个方案结合了批处理的界面友好性和PowerShell的强大文件操作能力,提供了安全、灵活的Chrome缓存清理方案。