Get-EventLog in PowerShell 7: Use Get-WinEvent Instead

Get-EventLog is a Windows PowerShell 5.1 cmdlet. It is not part of PowerShell 7, and the command to use in its place is Get-WinEvent:

PowerShell
Get-WinEvent -LogName Application -MaxEvents 10

On Windows, PowerShell 7 will still run Get-EventLog by loading it through the Windows PowerShell compatibility session, so an old script may keep working. It only reads the classic logs, though, and it cannot filter at the source. Below are the common Get-EventLog commands and what to write in their place. Get-WinEvent works the same in Windows PowerShell 5.1, so these run in either.

Get-WinEvent in Windows PowerShell 5.1: the ten largest event logs by record count, the last 1000 Application events grouped by provider, and the five newest MSSQLSERVER events

List the Event Logs

The old Get-EventLog -List becomes:

PowerShell
Get-WinEvent -ListLog * -ErrorAction SilentlyContinue |
    Where-Object RecordCount -gt 0 |
    Sort-Object RecordCount -Descending |
    Select-Object LogName, RecordCount -First 10

This shows the per-component Microsoft-Windows-* logs as well as Application, System and Security, which Get-EventLog cannot see. Security needs an elevated window. Check If PowerShell Is Running as Administrator shows how to check which one you are in.

Count Events by Source

PowerShell
Get-WinEvent -LogName Application -MaxEvents 1000 |
    Group-Object ProviderName -NoElement |
    Sort-Object Count -Descending

What Get-EventLog calls the Source, Get-WinEvent calls the ProviderName. -MaxEvents keeps it to the newest 1000 records, because the whole Application log can be tens of thousands.

Filter by Source or Message

Get-EventLog -Message has no direct match. If what you are after is one source, filter on the provider, which the event log service does for you and is fast:

PowerShell
Get-WinEvent -FilterHashtable @{ LogName = 'Application'; ProviderName = 'MSSQLSERVER' } -MaxEvents 10

If you really need to search the message text, do it after a time filter, so you are not reading the whole log:

PowerShell
Get-WinEvent -FilterHashtable @{ LogName = 'Application'; StartTime = (Get-Date).AddDays(-7) } |
    Where-Object Message -like '*MSSQLSERVER*' |
    Select-Object -First 10 TimeCreated, Id, ProviderName

If SQL Server is why you are in the Application log, Check When SQL Server Was Last Restarted on sqldba.blog uses the same events.

Show the Full Message

PowerShell
Get-WinEvent -FilterHashtable @{ LogName = 'Application'; ProviderName = 'MSSQLSERVER' } -MaxEvents 1 |
    Select-Object -ExpandProperty Message

-ExpandProperty prints the text on its own, without the column cutting it short. Format-List shows it with the other properties.

Parameter and Property Names

  • -Newest 10 becomes -MaxEvents 10.
  • -Source becomes ProviderName in the filter hashtable.
  • -EntryType Error becomes Level = 2 in the filter hashtable (1 Critical, 2 Error, 3 Warning, 4 Information).
  • -After becomes StartTime in the filter hashtable.
  • EventID, EntryType, TimeGenerated on the output become Id, LevelDisplayName, TimeCreated.

One difference to expect in scripts: when nothing matches, Get-WinEvent -FilterHashtable raises No events were found that match the specified selection criteria as an error. Add -ErrorAction SilentlyContinue where an empty result is normal. How to Show Event Logs in PowerShell goes further into filtering, remote machines and the slow and fast ways to filter.

The Original Get-EventLog Commands

If you are on Windows PowerShell 5.1 and only need the classic logs, these still work as they always did.

PowerShell
Get-EventLog -List
get event log types example
PowerShell
Get-EventLog -LogName Application | Group-Object -Property Source -NoElement | Sort-Object -Property Count -Descending
get application events by count
PowerShell
Get-EventLog -LogName Application -Newest 10 -Message *MSSQLSERVER*
show recent eventlog entries for sql server
PowerShell
Get-EventLog -LogName Application -Newest 1 -Message *MSSQLSERVER* | Select-Object Message
show recent sql server events

Related

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 *