Search Event Logs
Last updated
Last updated
PowerShell provides powerful tools for querying, filtering, and analyzing Windows event logs. Whether you're working with local or remote logs, PowerShell allows you to extract meaningful information and generate reports. This guide covers the use of Get-WinEvent
, Get-EventLog
, and advanced techniques like XPath queries and hash table filters.
Windows event logs store critical information about system events, security incidents, and application activities. PowerShell allows you to:
Query logs on local or remote systems.
Filter logs based on specific criteria.
Export logs to formats like CSV or HTML for further analysis.
Get-WinEvent
The Get-WinEvent
cmdlet is the modern way to query event logs. It supports both classic logs (System, Security, Application) and newer logs introduced in Windows Server 2008 and later.
Listing Event Logs
To list all available event logs:
To list logs starting with "S" on a remote computer:
Querying Specific Logs
To retrieve the last 20 events from the System log:
To export the last 2,000 events from the Application, Security, and System logs to a CSV file:
Get-WinEvent
Using Hash Tables
Hash tables allow you to filter logs based on specific properties. Supported properties include:
LogName
ProviderName
ID
Level
StartTime
EndTime
Example: Filter by Event ID To retrieve events with ID 4624 from the Security log:
Example: Filter by Time Range To retrieve Security log events between 5 and 3 days ago:
Example: Filter by Log Level To retrieve Warning events from the Application log:
Example: Filter on Remote Computer To retrieve the last 10 Critical, Error, and Warning events from the System log on a remote computer:
XPath queries allow for more complex filtering. You can use the Event Viewer to generate XPath queries and then use them in PowerShell.
Generating XPath Queries
Open Event Viewer.
Right-click a log and select Filter Current Log.
Configure the filter on the Filter tab.
Switch to the XML tab and copy the XML query.
Using XPath Queries in PowerShell
To retrieve Critical, Error, and Warning events from the System log in the last 24 hours:
Get-EventLog
(Legacy)The Get-EventLog
cmdlet is older and less efficient than Get-WinEvent
. It downloads entire logs before filtering, making it slower for remote queries.
Listing Event Logs
To list event logs and their settings:
Querying Specific Logs
To retrieve the last 20 events from the System log:
Filtering Logs
To retrieve Warning and Error events from the last 500 System log events:
To retrieve the last 10 user account creation events from the Security log:
PowerShell also provides cmdlets for managing event logs:
Clear-EventLog: Clears event logs.
Write-EventLog: Writes custom events to a log.
Limit-EventLog: Configures log size and retention policies.
Example: Clear Logs on a Remote Computer To clear the System and Application logs on a remote computer:
You can create and manage custom logs using Add-Content
and Import-Csv
.
Example: Append to a Custom Log