Updated Event Log Check

A few months ago, I wrote an entry about using Powershell to look at recent event logs. Since then, I’ve decided to make a minor improvement. The new function lets you specify the number of event logs to retrieve:

Function Show-Logs {
Param([int]$iEntries)

#default for number of entries is 5
if ($iEntries -lt 1) {$iEntries=5}

foreach ($log in (get-eventlog -list -asString)) {
   Write-Host $log -fore Green -back Black; Get-eventlog $log -newest $iEntries `
   |select TimeGenerated,EntryType,Source,EventID,Message | more
  }
}

I’ve added this function to my Powershell profile. The new syntax is Show-Logs [number-of-entries]. I’ve added code so that if you don’t specify the number of recent entries for each log, it will default to 5:

if ($iEntries -lt 1) {$iEntries=5}

Otherwise, the function will return the X number of recent event entries for each log file on your system.