How to Show Event Logs in PowerShell (Get-WinEvent)

📜Part of the PowerShell Complete Guide, every PowerShell post on this site, grouped by the job you are doing.
The short answer

Get-WinEvent -LogName System -MaxEvents 20 shows the 20 newest System events. Filter at the log with -FilterHashtable, not afterwards with Where-Object, or you pull the whole log into memory first.

Reading Windows event logs from PowerShell is one of those jobs where the obvious approach works fine on a small log and crawls on a real one. This covers the cmdlet to use, how to filter properly, and the older cmdlet you will still see in examples.

Listing The Logs

There are hundreds of logs on a modern Windows machine, most of them empty. Show the ones that actually have records in them:

PowerShell
Get-WinEvent -ListLog * -ErrorAction SilentlyContinue |
    Where-Object { $_.RecordCount -gt 0 } |
    Sort-Object RecordCount -Descending |
    Select-Object LogName, RecordCount -First 10
Get-WinEvent -ListLog in PowerShell showing the ten logs with the most records, from Microsoft-Windows-StorageManagement/Operational down to Application

Application, System and Security are the three classic logs. Security is missing from my list because it can only be read from an elevated window, and -ErrorAction SilentlyContinue keeps that from printing as an error. The long Microsoft-Windows-* names are per-component logs, and they are where the useful detail usually lives once you know which component you care about.

Reading Recent Events

PowerShell
Get-WinEvent -LogName System -MaxEvents 20

-MaxEvents is not optional in practice. Without it you are asking for tens of thousands of records.

Filtering Properly

This is the part that matters. There are two ways to filter and they perform very differently.

The slow way

PowerShell
# pulls the ENTIRE log into memory, then discards most of it
Get-WinEvent -LogName System | Where-Object { $_.Level -eq 2 }

The fast way

PowerShell
Get-WinEvent -FilterHashtable @{
    LogName   = 'System'
    Level     = 2, 3          # 2 = Error, 3 = Warning
    StartTime = (Get-Date).AddDays(-7)
} -MaxEvents 50

-FilterHashtable pushes the filter down to the event log service, so only matching records are ever returned. On a log with tens of thousands of records that is the difference between a command you wait for and one you do not.

Here are the ten newest errors in the System log, trimmed to three columns:

PowerShell
Get-WinEvent -FilterHashtable @{ LogName = 'System'; Level = 2 } -MaxEvents 10 |
    Select-Object TimeCreated, Id, ProviderName | Format-Table -AutoSize
PowerShell 7: Get-WinEvent -FilterHashtable with LogName System and Level 2 returns the ten newest errors, with event IDs 1796 TPM-WMI, 10010 DistributedCOM, 7023, 7011 and 7034 Service Control Manager and 2042 Windows Firewall

The level numbers are the ones to memorise: 1 Critical, 2 Error, 3 Warning, 4 Information, 5 Verbose. They are numbers in the filter and words in the output, which is mildly annoying and worth expecting.

Useful Filters

PowerShell
# errors and warnings from the last day
Get-WinEvent -FilterHashtable @{ LogName='System'; Level=1,2,3
                                 StartTime=(Get-Date).AddDays(-1) }

# one specific event ID
Get-WinEvent -FilterHashtable @{ LogName='System'; ID=6008 }

# everything from one source
Get-WinEvent -FilterHashtable @{ LogName='Application'; ProviderName='MsiInstaller' }

# unexpected shutdowns, tidied for reading
Get-WinEvent -FilterHashtable @{ LogName='System'; ID=6008 } -MaxEvents 10 |
    Select-Object TimeCreated, Id, LevelDisplayName, Message |
    Format-List
An empty result is not always an empty log. If no records match, Get-WinEvent -FilterHashtable raises “No events were found that match the specified selection criteria” as an error rather than returning nothing. In a script, add -ErrorAction SilentlyContinue so a quiet log does not read as a failure.

An event message rarely explains itself. Paste the ID, the source and the message into Claude and ask what it means and what to check next.

Reading A Remote Machine

PowerShell
Get-WinEvent -ComputerName SERVER01 -FilterHashtable @{
    LogName='System'; Level=1,2 } -MaxEvents 25

This needs the Remote Event Log Management firewall rule enabled on the target and appropriate permissions. The Security log additionally requires administrator rights, locally or remotely.

What About Get-EventLog?

You will find plenty of older examples using Get-EventLog:

PowerShell
Get-EventLog -LogName System -Newest 20

It still runs in Windows PowerShell 5.1. In PowerShell 7 it is not built in: on Windows it loads through the Windows PowerShell compatibility layer, a proxy module behind the scenes. It only reads the classic logs though, so it cannot see any of the Microsoft-Windows-* ones, and it has no equivalent of -FilterHashtable. Treat it as legacy: fine in an old script you are not touching, not what you would write today. Get-WinEvent does everything it does and more.

If SQL Server is part of your day job, my current work lives over at sqldba.blog: production DBA scripts, an error library and the SSMS guide.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *