PowerShell Script to Check Event Log Easily
Get-EventLog -LogName System -Newest 5 in PowerShell to check the latest 5 entries in the System event log.Examples
How to Think About It
Get-EventLog command with the -LogName parameter and specify how many recent entries to see with -Newest. This lets you quickly view recent events without scrolling through the entire log.Algorithm
Code
Get-EventLog -LogName System -Newest 5 | Format-Table -AutoSizeDry Run
Let's trace checking the latest 2 entries from the Application log.
Select Log
User chooses 'Application' log.
Run Command
PowerShell runs: Get-EventLog -LogName Application -Newest 2
Display Output
Shows 2 most recent Application log entries.
| Index | Time | EntryType | Source | Message |
|---|---|---|---|---|
| 54321 | Jun 10 09:55 | Error | Application Error | Faulting application name: example.exe... |
| 54320 | Jun 10 09:50 | Information | AppModel-Runtime | The application identity is... |
Why This Works
Step 1: Get-EventLog Command
The Get-EventLog cmdlet reads Windows event logs by specifying the log name.
Step 2: Newest Parameter
The -Newest option limits output to the most recent entries, making it easier to see current events.
Step 3: Output Formatting
Using Format-Table -AutoSize neatly arranges the output for easy reading.
Alternative Approaches
Get-WinEvent -LogName System -MaxEvents 5 | Format-Table -AutoSizeGet-EventLog -LogName System -Newest 10 | Where-Object { $_.EventID -eq 7036 }
Complexity: O(n) time, O(n) space
Time Complexity
The command reads through the event log entries, which can be large, so time grows linearly with the number of entries requested.
Space Complexity
Memory usage depends on how many entries are retrieved; limiting with -Newest keeps space usage low.
Which Approach is Fastest?
Using Get-EventLog with -Newest is fast for recent entries; Get-WinEvent offers more features but can be slower.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Get-EventLog -Newest | O(n) | O(n) | Quick recent event checks |
| Get-WinEvent -MaxEvents | O(n) | O(n) | Detailed and newer event logs |
| Get-EventLog with filtering | O(n) | O(n) | Specific event searches |
-Newest to quickly see the latest events without scrolling through the entire log.-LogName, causing errors or no output.