PowerShell Script to Show Last 5 Errors in Event Log

The Windows System eventlog is one of the most powerful components of the operating system because it keeps a log of everything that happens. The main thing that you would be interested in is errors. You could locate the eventlog and search through its' tens of thousands of entries for the word "error", but there is an easier way.

PowerShell is a more powerful replacement for the command shell. The best way to use PowerShell is to create and run scripts. Once you've written a script, you can save it to use again and again, on your machine and others. If you are not familiar with powershell, read Introduction to Windows PowerShell

PowerShell is so powerful that they put a few obsticals to block hackers from using it. 1. In the task bar Search box, type "powershell". In the menu that appears, right-click on Windows PowerShell, and in the menu that appears, select "Run as administrator". 2. Because of system security, powershell's execution policy may not allow you to run scripts. To change PowerShell's execution policy, at the powershell prompt enter Set-ExecutionPolicy Unrestricted.

To create a PowerShell script open a basic ASCII text editor like Windows Notepad and type in the following comand:

get-EventLog -Logname System -EntryType Error -Newest 5

This opens the log named "System" and checks for "Error" entries, listing the 5 most recent entirs.

Save the file extension .ps1 for example getErrors.ps1. To run the script, navigate to the folder you saved it in and enter the script name. Don't forget to prefix the script name with .\ so PowerShell looks in the current directory for the file.

Shown above is the result of running the script. Now for the meaning of the errors returned by PowerShell, that is an article for another time.