Powershell

How to Manage Windows Event Logs With PowerShell?

Power is a scripting language for the configuration and automation of tasks. Windows PowerShell creates an event log to record the events of PowerShell. The main focus of the event log is to get all activity logs and troubleshoot them if any error occurs.

This blog will cover detailed information about managing event logs with PowerShell.

How to Manage Windows Event Logs With PowerShell?

PowerShell has over 200 different cmdlets that are used according to the requirement. For event logs, Get-Eventlog and Get-WinEvent cmdlets are used to get logs of events. Users can get information about the system that needs to be fixed. Using the console, the user can check events on both local and remote computers. PowerShell generates automatic reports for important events and this specification is very helpful in maintaining server health monitoring. Each event falls under the category of application, security, or system and event logs contain the Date, Time, Level, and General & Details. Event logs can be managed by:

Access Event Viewer Manually

Users can check the event viewer to monitor logs by pressing the Start button, searching Event Viewer, and selecting Run as administrator:


As a result, it will open the Event Viewer window as provided below. Now, click on the “Windows Logs”: option and check out the logs according to the desired:

Access Event Logs Using PowerShell

Users can view event logs using the following cmdlets in PowerShell:

Get-EventLog -list

 

OR

Another command that is used to list the event logs is:

Get-WinEvent -ListLog * | where {$_.RecordCount -gt 0}

 

The Get-WinEvent works with Windows eventing technology.

To check the list of particular event logs along with the details, use the following cmdlet:

Get-EventLog application -newest 1 | Get-Member

 

In Get-EventLog users can use -After and -Before to filter the results by providing data.

Access Event Log of Specified Date

First, users can retrieve the current date and time by running the provided cmdlet:

Get-Date

 

Then, use the following command to check the logs of a particular application on a specified date for the first time:

Get-EventLog system -after (get-date).AddDays(-1) | where {$_.InstanceId -eq 7001}

 

The AddDays(-1) flag is used to display the day before the current day.

Property Value of Recent Event

Users can check the most recent event log value with the help of the following command:

Get-EventLog -LogName System -Newest 1
Select-Object -Property *

 

Access Custom Event log

Users can generate new events using the “New-EventLog” command and make specified entries of new events by running the “Write-EventLog” command:

New-EventLog -LogName newgen-Source New_eventlog

Write-EventLog -LogName "newgen" -Source "New_eventlog" -EventId 1001 -EntryType Information -Message " log entry."

 

To check specified event logs, execute the provided command:

Get-WinEvent -LogName "newgen"

 

Next, to clear all entities of the event logs use the “Clear-Eventlog” cmdlet and then verify it by executing the “Get-WinEvent” command:

Clear-EventLog -LogName "newgen"

Get-WinEvent -LogName "newgen"

 

The above output shows that the “newgen” event log has been deleted.

Conclusion

Managing Windows event logs with PowerShell helps to troubleshoot events of the system. Event logs can be accessed and filtered using “Get-Eventlog” and “Get-WinEvent” cmdlets. The “Get-WinEvent” cmdlet is used for accessing event logs in recent versions of Windows while “GetEventLog” is compatible with older versions of Windows. Users can create a script to schedule regular event checks and generate an event log if any event is critical. This blog demonstrated detailed instructions to manage event logs with PowerShell.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.