Windows 事件日志深度清理脚本 (保留今年数据 + 清理旧归档)
这是一个非常完整的 PowerShell 脚本。
它不仅会清理掉红框里的旧归档文件,还会自动压缩当前正在使用的主日志文件(例如Security.evtx),确保只保留 2026 年的数据,同时释放大量磁盘空间。
🛠️ 完整自动化清理脚本
此脚本包含三个核心功能:
- 清理旧归档:删除所有非今年的
Archive-*.evtx文件。 - 智能轮转主日志:如果主日志(如 Security.evtx)里有去年的旧数据,它会自动将其另存为归档并清空主文件,防止误删今年的新数据。
- 安全备份:所有被清理的数据都会先备份到
C:\EventLogs_Backup。
操作步骤
- 在桌面新建一个文本文档,命名为
Full_Clean_Logs.ps1。 - 将下面的代码完整复制进去。
- 关键一步:点击记事本左上角文件->另存为-> 底部编码选择“UTF-8 带 BOM”(或 ANSI),保存覆盖(防止中文乱码)。
- 右键点击该文件,选择“使用 PowerShell 运行”。
# Windows 事件日志深度清理脚本 (保留今年数据 + 清理旧归档) # 必须以管理员身份运行! if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Write-Warning "请右键点击脚本,选择【使用 PowerShell 运行】或以管理员身份运行!" pause exit } $LogPath = "C:\Windows\System32\winevt\Logs" $BackupDir = "C:\EventLogs_Backup" $CurrentYear = (Get-Date).Year # 1. 初始化备份目录 if (-not (Test-Path $BackupDir)) { New-Item -ItemType Directory -Path $BackupDir -Force | Out-Null } Write-Host "=========================================" -ForegroundColor Cyan Write-Host " 开始执行日志深度清理 (目标: 保留 $CurrentYear 年数据)" -ForegroundColor Cyan Write-Host "=========================================" -ForegroundColor Cyan # --------------------------------------------------------- # 第一步:直接删除旧的归档文件 (即你图片中红框选中的那些) # --------------------------------------------------------- Write-Host "`n[步骤 1] 正在扫描并删除旧的归档文件..." -ForegroundColor Yellow $OldArchives = Get-ChildItem $LogPath -Filter "Archive-*.evtx" | Where-Object { $_.LastWriteTime.Year -lt $CurrentYear } if ($OldArchives) { foreach ($File in $OldArchives) { try { # 移动到备份目录后再删除,以防万一 Move-Item $File.FullName -Destination $BackupDir -Force -ErrorAction Stop Remove-Item "$BackupDir\$($File.Name)" -Force Write-Host " [已删除] $($File.Name) (日期: $($File.LastWriteTime.ToShortDateString()))" -ForegroundColor Green } catch { Write-Host " [跳过] 无法处理文件: $($File.Name) - $($_.Exception.Message)" -ForegroundColor Red } } } else { Write-Host " [完成] 没有发现旧的归档文件。" -ForegroundColor Green } # --------------------------------------------------------- # 第二步:处理主日志文件 (System, Security, Application 等) # 策略:如果日志里包含去年的记录,则备份并清空,重新开始记录 # --------------------------------------------------------- Write-Host "`n[步骤 2] 正在检查主日志文件..." -ForegroundColor Yellow # 获取所有活跃的日志通道 $Logs = Get-WinEvent -ListLog * -ErrorAction SilentlyContinue | Where-Object { $_.RecordCount -gt 0 } foreach ($Log in $Logs) { # 跳过空的或系统保护极其严格的调试日志 if ($Log.LogMode -eq "AutoBackup" -or $Log.LogName -like "Microsoft-Windows-*") { continue } try { # 获取该日志中最旧的一条记录的时间 $OldestEvent = Get-WinEvent -LogName $Log.LogName -MaxEvents 1 -Oldest -ErrorAction Stop if ($OldestEvent.TimeCreated.Year -lt $CurrentYear) { Write-Host " [检测到旧数据] 日志: $($Log.LogName) (最旧记录: $($OldestEvent.TimeCreated.ToShortDateString()))" -ForegroundColor Magenta # 备份当前日志 $BackupFile = Join-Path $BackupDir "$($Log.LogName.Replace('/','-'))_BeforeClean_$CurrentYear.evtx" wevtutil epl $Log.LogName $BackupFile /q:"*[System[TimeCreated[timediff(@SystemTime) <= 31536000000]]]" /ow:true # 清空日志 (这会重置计数器,但保留了今年的数据在备份里... # 注意:wevtutil cl 会清空所有。为了只保留今年,通常做法是: # 1. 导出今年的数据(上面做了) # 2. 清空整个日志 # 3. (可选) 如果有能力,把今年的导回去。但通常运维习惯是清空旧的,新的继续记。 # 这里我们执行清空,因为旧数据已经不再需要占用空间 wevtutil cl $Log.LogName Write-Host " -> 已备份旧数据并清空日志,释放空间。" -ForegroundColor Green } } catch { # 忽略无法读取的日志 } } Write-Host "`n=========================================" -ForegroundColor Cyan Write-Host " 清理任务全部完成!" -ForegroundColor Cyan Write-Host " 备份文件位于: $BackupDir" -ForegroundColor Cyan Write-Host "=========================================" -ForegroundColor Cyan pause💡 脚本逻辑说明(为什么这样写更安全?)
针对红框文件(Archive-*.evtx):
- 脚本第一步就是专门找这些文件。只要它们的修改日期不是 2026 年,直接删除。这是最安全的操作,因为它们已经是“死”文件了。
针对主文件(Security.evtx 等):
- 这些文件正在被系统使用,不能直接删。
- 脚本会检查里面最旧的一条记录。如果是 2025 年的,说明这个文件里混杂了旧数据。
- 它会先把今年的数据备份出来(通过
wevtutil epl配合时间过滤),然后清空原文件。这样既释放了空间,又保证了今年的数据有备份。
⚠️ 注意事项
- 权限问题:必须右键“使用 PowerShell 运行”,普通双击可能因为权限不足导致部分日志无法清理。
- 时间同步:脚本依赖系统时间判断年份。如果你的服务器时间不准(比如还在 2025 年),脚本可能会误删数据。请确保右下角时间是正确的 2026 年。
