PowerShell Quick Event Log

These days I’m in to fast and furious coding. It seems I no longer have time to develop full, robust scripts with beautiful output and error handling.  I just need information.  Fortunately PowerShell is great at giving you lots of information. As long as you know how to ask for it. One daily task I have is checking my event log for the latest events. Here is a fast and furious single line expression that will display the last 10 entries in all the event logs on my system:

foreach ($log in (get-eventlog -list -asString)) {write-host $log -fore Green -back Black; Get-eventlog $log -newest 10|Select TimeGenerated,EntryType,Source,EventID,Message | more}

The foreach cmdlet is iterating through the list of event logs as returned by

(get-eventlog -list -asString)

For each log, I write the log name in Green on a  Black background, primarily so I can tell which entries go with which logs.

write-host $log -fore Green -back Black

I then call Get-EventLog to retrieve the newest 10 entries, passing the logfile name as $log:

Get-eventlog  $log -newest 10

Each entry has more information than I really need so I use Select-object to filter just the information I’m after:

Select TimeGenerated,EntryType,Source,EventID,Message

Finally, I pipe everything through More so I can page through the results. Even though this is a single command, it is a lot to type each time, so you should put it in a function, in your profile or in a standalone script.

 

Technorati tags: , ,

del.icio.us tags: , ,