专门针对 Windows Server 2012 设计。它会自动扫描所有磁盘的卷影副本(Shadow Copies)占用情况,并安全地清理旧数据,最后汇报释放了多少空间。
📋 脚本功能
- 自动扫描:检查所有磁盘的卷影存储占用。
- 安全清理:删除除最新一份以外的所有旧卷影副本。
- 空间限制:将卷影副本最大占用限制为磁盘的5%(防止再次爆满)。
- 结果汇报:显示清理前后的空间变化。
🚀 使用方法
- 在桌面新建一个文本文件,命名为
Clean_SVI.ps1。 - 将下方代码完整复制进去:
# Windows Server 2012 System Volume Information 安全清理脚本 # 必须以管理员身份运行! if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Write-Warning "请右键点击 PowerShell,选择【以管理员身份运行】!" pause exit } Write-Host "========================================" -ForegroundColor Cyan Write-Host " System Volume Information 安全清理工具" -ForegroundColor Cyan Write-Host "========================================" -ForegroundColor Cyan Write-Host "" # 1. 显示当前卷影存储占用 Write-Host "[1/3] 正在检查卷影副本占用情况..." -ForegroundColor Yellow vssadmin list shadowstorage | Select-String -Pattern "卷|Volume|已用|Used|最大|Maximum" | ForEach-Object { Write-Host $_.Line.Trim() } Write-Host "" # 2. 清理旧卷影副本(保留最新的一个) Write-Host "[2/3] 正在清理旧卷影副本(保留最新1份)..." -ForegroundColor Yellow try { # 获取所有卷影副本 $shadows = Get-WmiObject -Class Win32_ShadowCopy if ($shadows) { $count = ($shadows | Measure-Object).Count Write-Host "发现 $count 个卷影副本,正在清理..." # 按安装日期排序,保留最新的,删除其余的 $shadows | Sort-Object InstallDate -Descending | Select-Object -Skip 1 | ForEach-Object { Write-Host " 删除: $($_.DeviceObject)" -ForegroundColor Gray $_.Delete() | Out-Null } Write-Host "清理完成!" -ForegroundColor Green } else { Write-Host "未发现卷影副本,无需清理。" -ForegroundColor Green } } catch { Write-Warning "清理过程出现警告(通常是因为文件被占用),可忽略。" } Write-Host "" # 3. 限制卷影存储大小(防止未来爆满) Write-Host "[3/3] 正在设置卷影存储上限(磁盘的 5%)..." -ForegroundColor Yellow $drives = Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" foreach ($drive in $drives) { $driveLetter = $drive.DeviceID $maxSize = [math]::Round($drive.Size * 0.05 / 1GB, 2) Write-Host " 设置 $driveLetter 上限为 ${maxSize}GB..." -ForegroundColor Gray vssadmin resize shadowstorage /on=$driveLetter /for=$driveLetter /MaxSize=${maxSize}GB | Out-Null } Write-Host "" Write-Host "========================================" -ForegroundColor Cyan Write-Host " 操作完成!请检查磁盘空间是否释放。" -ForegroundColor Green Write-Host "========================================" -ForegroundColor Cyan pause- 右键点击
Clean_SVI.ps1→ 选择使用 PowerShell 运行(或右键 PowerShell → 以管理员身份运行 → 输入.\Clean_SVI.ps1)。
⚠️ 注意事项
- 不要手动删除文件夹:脚本只清理里面的“卷影副本”,不会动系统索引等核心文件。
- 服务器环境:如果你的服务器正在运行SQL Server或Exchange,它们可能正在使用卷影副本进行备份。建议在非备份窗口期运行此脚本。
- 保留策略:脚本默认保留1份最新副本。如果你需要保留更多,修改代码中的
Select-Object -Skip 1为Skip 3(保留3份)。