如何使用命令行清除Windows事件日志?

通常我可以打开计算机pipe理控制台,进入事件查看器pipe理单元,打开Windows日志文件夹,右键单击Application/Security/Setup/System子文件夹,select清除日志,然后按ClearSave and Clearbutton。

有足够的权利,我怎么能通过使用命令行达到相同的效果,而没有提出确认请求?

电源shell。

 PS C:\>Clear-Eventlog -Log Application, System 

默认不会提示您,但是如果您想要提示您可以提供-Confirm开关。

编辑:

 Get-WinEvent -ListLog Application,Setup,Security -Force | % { Wevtutil.exe cl $_.Logname } 

根据评论,这应该得到操作和pipe理日志。

wevtutil enum-logs将枚举系统中的所有日志,而wevtutil clear-log将清除日志。 对于你的情况,这将是:

 wevtutil clear-log Application wevtutil clear-log Security wevtutil clear-log Setup wevtutil clear-log System 

您也可以使用wevtutil clear-log System /backup:backup.evtx

对于你想清除所有日志的情况

 for /f %x in ('wevtutil el') do wevtutil cl "%x" 

从这里提取。

以下PowerShell以编程方式清除本地机器上的所有事件日志, 包括操作/debugging/安装日志(不需要实例化“wevtutil”进程)。 要清除一个日志,相应地修改代码。 然而,这并不完美,有时候debugging日志会被某些东西保持打开状态,而这不会产生任何错误。

 $EventLogs=Get-WinEvent -Force -ListLog * $EventSession=new-object System.Diagnostics.Eventing.Reader.EventLogSession foreach ($Log in $EventLogs) { if ($Log.IsEnabled) { if ($Log.RecordCount -gt 0) { if ($Log.LogType -eq "Debug") { $Log.IsEnabled=$false $Log.SaveChanges() $EventSession.ClearLog($Log.LogName) $Log.IsEnabled=$true $Log.SaveChanges() } else { $EventSession.ClearLog($Log.LogName) } } } 

这是如何通过PowerShell清除所有事件日志,请确保您以pipe理员身份运行它

wevtutil el | Foreach-Object {wevtutil cl "$_"}