To search Windows event logs efficiently with PowerShell, use Get-WinEvent FilterHashtable queries: Get-WinEvent -FilterHashtable. You pass a hash table such as @{ LogName='System'; Level=2; StartTime=(Get-Date).AddDays(-1) } and Windows returns only the matching events: here, errors from the System log in the last 24 hours. Microsoft's documentation states that these filters "are more efficient than using the Where-Object cmdlet", because they are applied as events are retrieved instead of after every event has been loaded. This guide covers the valid keys, the numbers behind levels and keywords, ready-to-use filters for reboots, service installs and failed sign-ins, and how to run the same query on many computers.
Get-WinEvent FilterHashtable basics
A filter hash table is a set of key = value pairs. Put them on one line separated by semicolons, or on separate lines without semicolons:
# One line
Get-WinEvent -FilterHashtable @{ LogName='Application'; Level=2 } -MaxEvents 20
# Several lines (easier to read and build up step by step)
Get-WinEvent -FilterHashtable @{
LogName = 'System'
Level = 2, 3 # Error and Warning
StartTime = (Get-Date).AddDays(-1)
} | Select-Object TimeCreated, Id, ProviderName, LevelDisplayName, Message
By default Get-WinEvent returns events newest first. -MaxEvents limits how many are returned; without it, you get every match. Microsoft's FilterHashtable article recommends building the table one pair at a time, starting with LogName, so you can see which key removes the results you expected.
These are the keys the -FilterHashtable parameter accepts, according to the Get-WinEvent reference:
| Key | Value type | Wildcards? | Use it for |
|---|---|---|---|
LogName | String[] | Yes | System, Application, Security or a named log |
ProviderName | String[] | Yes | The event source, for example Service Control Manager |
Path | String[] | No | Saved .evtx, .evt or .etl files |
Keywords | Long[] | No | Keyword masks such as Audit Failure (numbers, not names) |
ID | Int32[] | No | One or more event IDs |
Level | Int32[] | No | Severity (numbers, not names) |
StartTime / EndTime | DateTime | No | A time window |
UserID | SID | No | A SID or a domain account name |
Data | String[] | No | Unnamed event data, for example in classic logs |
<named-data> | String[] | No | A named field in the event data (PowerShell 6 and later) |
SuppressHashFilter | Hashtable | No | Events to exclude |
Three rules from the documentation prevent most mistakes: keys and values are case-insensitive, each key may appear only once per hash table, and wildcards work only in LogName and ProviderName values. To list several IDs or levels, give the key an array (ID = 41, 1074, 6008) rather than repeating it.
Level and Keywords take numbers, not names
Event Viewer shows levels and keywords as words, but the hash table needs their numeric values. Microsoft notes that using a string for Level or Keywords produces an error. The level values come from the StandardEventLevel enumeration:
| Level | Value |
|---|---|
| Critical | 1 |
| Error | 2 |
| Warning | 3 |
| Informational | 4 |
| Verbose | 5 |
| LogAlways | 0 |
For keywords, the commonly needed values are AuditSuccess (9007199254740992), AuditFailure (4503599627370496) and EventLogClassic (36028797018963968). If you'd rather not paste long numbers, convert the enumeration name with the value__ property, as Microsoft's article shows:
$failure = [System.Diagnostics.Eventing.Reader.StandardEventKeywords]::AuditFailure
Get-WinEvent -FilterHashtable @{ LogName='Security'; Keywords=$failure.value__ } -MaxEvents 10
Exclude noise with SuppressHashFilter
Sometimes it is easier to say what you don't want. SuppressHashFilter takes its own hash table of events to leave out. This returns the last two days of the Application log without Informational (level 4) events:
Get-WinEvent -FilterHashtable @{
LogName = 'Application'
StartTime = (Get-Date).AddDays(-2)
SuppressHashFilter = @{ Level = 4 }
}
Practical filters for administrators
The event IDs below come from Microsoft documentation, cited in the sources. Microsoft also warns that the same event ID can be used by different sources, so these examples print ProviderName; check it before you draw conclusions.
Why did this computer restart?
Microsoft's guide to unexpected reboots lists the System log events that tell a planned restart from an unexpected one. Event 1074 (source User32) names the process and account that initiated a restart and the reason. Event 41 (Kernel-Power) means the system rebooted without cleanly shutting down first, and event 6008 (EventLog) records that the previous shutdown was unexpected. Event 1001 (WER-SystemErrorReporting) means the computer rebooted from a bug check, and 6005 marks the start of the Event Log service.
Get-WinEvent -FilterHashtable @{
LogName = 'System'
ID = 41, 1001, 1074, 6005, 6008
StartTime = (Get-Date).AddDays(-14)
} | Select-Object TimeCreated, Id, ProviderName, Message |
Format-Table -Wrap
A normal restart usually shows a 1074 shortly before the restart; Microsoft notes that with an unexpected reboot "there usually isn't an Event ID 1074 log entry". Pair this with our guide to getting the last boot time with PowerShell when you need the exact boot time and uptime.
Which services were installed recently?
Event 7045 from the Service Control Manager reads "A service was installed in the system" and includes the service name, file name, start type and account. Microsoft's reboot guide uses it to spot driver or software installs that happened just before problems started:
Get-WinEvent -FilterHashtable @{
LogName = 'System'
ProviderName = 'Service Control Manager'
ID = 7045
StartTime = (Get-Date).AddDays(-30)
} | Select-Object TimeCreated, Message
To see the current state of a service you find here, use how to check service status with PowerShell.
Failed sign-ins (event 4625)
Event 4625, "An account failed to log on", is written to the Security log of the computer where the logon attempt was made. Microsoft lists it under the Audit Logon and Audit Account Lockout audit subcategories. Run the query from an elevated PowerShell session: the Get-WinEvent reference warns that without administrator rights you may see errors that you can't retrieve information about a log.
Get-WinEvent -FilterHashtable @{
LogName = 'Security'
ID = 4625
StartTime = (Get-Date).AddHours(-24)
} -MaxEvents 50 | ForEach-Object {
$data = ([xml]$_.ToXml()).Event.EventData.Data
[pscustomobject]@{
Time = $_.TimeCreated
Account = ($data | Where-Object Name -eq 'TargetUserName').'#text'
LogonType = ($data | Where-Object Name -eq 'LogonType').'#text'
Source = ($data | Where-Object Name -eq 'IpAddress').'#text'
Status = ($data | Where-Object Name -eq 'Status').'#text'
}
}
ToXml() returns the event's full XML, which is the reliable way to read named fields such as TargetUserName. Microsoft's event reference lists the logon types (for example 2 for Interactive, 3 for Network and 10 for RemoteInteractive) and the status codes, such as 0xC000006A for a bad password. Treat the output as data to investigate, not as a verdict: a burst of failures can be a mistyped saved password just as easily as an attack.
In PowerShell 6 and later you can also filter on a named data field directly. Microsoft's example matches a field called Service in the Application log:
Get-WinEvent -FilterHashtable @{ LogName='Application'; 'Service'='Bits' }
Search a saved log file
The Path key reads exported .evtx files, which is handy when a user sends you a log:
Get-WinEvent -FilterHashtable @{ Path='C:\Logs\System.evtx'; Level=1, 2 }
Query event logs on remote computers
Get-WinEvent has a -ComputerName parameter. The reference says it "doesn't rely on PowerShell remoting", accepts only one computer name at a time, and needs the firewall to allow remote access to the event log service. For several computers, loop over them or run the query remotely with Invoke-Command, which returns a PSComputerName property on every result:
$computers = 'PC01', 'PC02', 'PC03'
Invoke-Command -ComputerName $computers -ErrorAction SilentlyContinue -ScriptBlock {
Get-WinEvent -FilterHashtable @{
LogName = 'System'
ID = 41, 6008
StartTime = (Get-Date).AddDays(-7)
} -ErrorAction SilentlyContinue
} | Select-Object PSComputerName, TimeCreated, Id, ProviderName |
Sort-Object PSComputerName, TimeCreated
This lists unexpected restarts in the last week across the computers you name. A computer that returns nothing either had no matching events or could not be reached, so check unreachable machines separately. Invoke-Command needs WinRM on the targets; our guide to PowerShell remote management without RDP compares WinRM, SSH remoting and agent-based options.
Running event log queries through an RMM agent
Every filter in this guide only reads, which makes it a good first step before any change, in line with our list of read-only PowerShell commands. With EclipseRMM's PowerShell RMM, you can run authorized PowerShell such as these Get-WinEvent queries on enrolled Windows devices through the installed agent, and review the output, errors and completion status in the portal. The agent service runs as LocalSystem, so commands run with high privileges; review each command before you submit it. A device that is offline cannot be assumed to have run a request, so check the completion record. If you find a failing service, see how to restart a service remotely with PowerShell for a safe way to act on it.
FAQ
Why is FilterHashtable faster than Where-Object?
Microsoft explains that the filter is applied as objects are retrieved, while Where-Object retrieves every event first and then filters them. On large logs that difference matters.
Should I use Get-EventLog instead?
The documentation says Get-WinEvent is designed to replace Get-EventLog on Windows Vista and later, and that Get-EventLog reads only classic logs and is kept for backward compatibility. Microsoft's FilterHashtable article also describes Get-EventLog as an option "prior to PowerShell 6", so use Get-WinEvent if your scripts must run in both Windows PowerShell 5.1 and PowerShell 7.
Can I use FilterXPath or FilterXml instead?
Yes. -FilterXPath takes an XPath query, and -FilterXml takes a structured XML query; Microsoft suggests building the XML with Event Viewer's "Create Custom View" or "Filter Current Log" dialog and copying it from the XML tab. Hash tables are easier to read; XML queries can express more complex conditions.
Is there a limit on how many logs I can query?
The reference notes that the underlying Windows API has a limit of 256 logs per query. If you need every log, loop through them one at a time.
Sources
- Get-WinEvent, Microsoft Learn
- Creating Get-WinEvent queries with FilterHashtable, Microsoft Learn
- Troubleshoot unexpected reboots using system event logs, Microsoft Learn
- 4625(F) An account failed to log on, Microsoft Learn
- EventLogRecord class (ToXml, TimeCreated, ProviderName), Microsoft Learn
- Invoke-Command, Microsoft Learn