Search code examples
windowspowershellbatch-fileloggingevent-viewer

How to clear Windows event logs without showing any error messages?


I'm creating a Windows event log-clearing software.

Whenever you clear logs from the command-line, there are always a few logs that can't be cleared, because either newer versions of Windows don't use the services those logs were meant for anymore, or they are important system logs that are almost always being written to.

However, when the Windows event utility tool finds out it can't clear those logs, it relays an error message to the user. And because I'm trying to make this Batch file into an actual software, I would rather not have a bunch of messy error messages on the screen, for the sake of the user experience.

The obvious answer to this problem would be to have the batch file start a separate background batch script to clear the logs, where the user wouldn't see any error messages, but modern-day anti-virus really don't like batch scripts, and flag them almost every time, so I can't do that.

I tried to instead create a powershell script (Much more trusted by anti-virus.) to clear the logs. But because of the nature of powershell, even if it runs completely in the background, without a console, it still relays error messages in the form of VBS XMessages.

If it's any useful, here's the two lines of code in the powershell script:

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

And for those who are wondering, yes, I am clearing logs with administrative privileges.

I need one of the following:

  • Another way to clear Windows event logs, that doesn't show error messages, that I can integrate into the software I'm creating.

  • A way to forcefully clear those Windows event logs that normally can't be cleared.

  • A way to have a batch script run without anti-virus having a stroke.

  • A way to make the powershell script run completely in the background, without event displaying error messages.


Solution

  • To any who happened to stumble upon this question, the answer is in the comments section, as credit goes to @vonPryz, who mentioned that Clear–EventLog would work better than wevtutil, and it does.