自动清理系统还原点脚本

wen 实用脚本 2

本文目录导读:

自动清理系统还原点脚本

  1. PowerShell 脚本(推荐)
  2. 使用WMIC命令行(兼容性好)
  3. 高级PowerShell脚本(可配置)
  4. 设置计划任务(自动执行)
  5. 使用方法:
  6. 注意事项:

PowerShell 脚本(推荐)

# 清理所有系统还原点(保留最新的)
Write-Host "正在清理系统还原点..." -ForegroundColor Yellow
try {
    # 获取所有还原点
    $restorePoints = Get-ComputerRestorePoint
    if ($restorePoints.Count -gt 1) {
        # 保留最新的还原点
        $latestPoint = $restorePoints | Sort-Object -Property CreationTime -Descending | Select-Object -First 1
        foreach ($point in $restorePoints) {
            if ($point.SequenceNumber -ne $latestPoint.SequenceNumber) {
                try {
                    Remove-ComputerRestorePoint -RestorePoint $point.SequenceNumber -Confirm:$false
                    Write-Host "已删除还原点: $($point.Description) - $($point.CreationTime)" -ForegroundColor Green
                }
                catch {
                    Write-Host "删除还原点失败: $($point.Description)" -ForegroundColor Red
                }
            }
        }
        Write-Host "清理完成!保留最新还原点: $($latestPoint.Description)" -ForegroundColor Green
    }
    elseif ($restorePoints.Count -eq 1) {
        Write-Host "只有一个还原点,无需清理。" -ForegroundColor Yellow
    }
    else {
        Write-Host "没有找到任何还原点。" -ForegroundColor Yellow
    }
}
catch {
    Write-Host "错误: $_" -ForegroundColor Red
}

使用WMIC命令行(兼容性好)

@echo off
echo 正在清理系统还原点...
:: 禁用系统保护
wmic.exe /Namespace:\\root\default Path SystemRestore Call Disable "%SystemDrive%"
:: 删除所有还原点(通过禁用/启用触发)
wmic.exe /Namespace:\\root\default Path SystemRestore Call Enable "%SystemDrive%"
:: 重新启用系统保护(设置使用空间为5%)
vssadmin.exe Resize ShadowStorage /For=%SystemDrive% /On=%SystemDrive% /MaxSize=5%
echo 清理完成!
pause

高级PowerShell脚本(可配置)

param(
    [int]$DaysToKeep = 30,           # 保留最近几天的还原点
    [int]$MaxCount = 3,              # 最大保留数量
    [switch]$KeepLatest = $true      # 是否始终保留最新的
)
Write-Host "系统还原点清理工具" -ForegroundColor Cyan
Write-Host "="*40
try {
    $restorePoints = Get-ComputerRestorePoint | Sort-Object CreationTime -Descending
    if ($restorePoints.Count -eq 0) {
        Write-Host "没有找到还原点。" -ForegroundColor Yellow
        exit
    }
    Write-Host "当前还原点数量: $($restorePoints.Count)" -ForegroundColor Yellow
    $cutoffDate = (Get-Date).AddDays(-$DaysToKeep)
    $deletedCount = 0
    foreach ($point in $restorePoints) {
        $shouldDelete = $false
        # 根据天数判断
        if ($point.CreationTime -lt $cutoffDate) {
            $shouldDelete = $true
        }
        # 保留最新的
        if ($KeepLatest -and $deletedCount -eq 0) {
            $shouldDelete = $false
        }
        # 限制最大数量
        $remainingCount = $restorePoints.Count - $deletedCount
        if ($remainingCount -le $MaxCount) {
            break
        }
        if ($shouldDelete) {
            try {
                Remove-ComputerRestorePoint -RestorePoint $point.SequenceNumber -Confirm:$false
                Write-Host "已删除: $($point.Description) ($($point.CreationTime))" -ForegroundColor Green
                $deletedCount++
            }
            catch {
                Write-Host "删除失败: $($point.Description)" -ForegroundColor Red
            }
        }
    }
    Write-Host "清理完成!删除了 $deletedCount 个还原点。" -ForegroundColor Cyan
    Write-Host "剩余还原点数量: $(Get-ComputerRestorePoint).Count" -ForegroundColor Yellow
}
catch {
    Write-Host "错误: $_" -ForegroundColor Red
    exit 1
}

设置计划任务(自动执行)

# 创建计划任务脚本(保存为 Set-Schedule.ps1)
$action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-File C:\Scripts\CleanRestorePoints.ps1"
$trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Sunday -At 3am
$principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries
Register-ScheduledTask -TaskName "清理系统还原点" -Action $action -Trigger $trigger -Principal $principal -Settings $settings
Write-Host "计划任务已创建!每周日凌晨3点自动执行。" -ForegroundColor Green

使用方法:

  1. 保存脚本:将以上任一段代码保存为 .ps1 文件(如 CleanRestore.ps1

  2. 以管理员身份运行

    # 右键点击 PowerShell -> 以管理员身份运行
    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
    .\CleanRestore.ps1
  3. 直接运行:如果是bat脚本,直接右键以管理员身份运行

注意事项:

  • ⚠️ 必须以管理员权限运行
  • 💾 建议保留至少1-2个最新的还原点
  • 🛡️ 清理前最好手动创建新的还原点作为备份
  • ⏰ 建议在系统空闲时运行

推荐使用第一个PowerShell脚本,它最安全且保留了最新的还原点。

抱歉,评论功能暂时关闭!