Windows 事件日志深度清理脚本 (保留今年数据 + 清理旧归档)

发布时间:2026/7/10 4:08:28

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 年。

相关新闻