Powershell: Logs script assistance


I am trying to enumerate all the event logs on a server and get 10 newest events from each log. I have something like this. I need some help with this. How do I enumerate the different logs such as System, Application, Directoryservice, FileReplication etc...

I need to replace $Logs with some dynamic code that will enumerate all the event logs on a machine.

$Logs ="System", "Application"

Foreach ($log in $logs) {
#Pull 10 newest events and store it in a file.

Thanks for your assistance.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You can use Get-WinEvent -ListLog *.  A single command would be like:
Get-WinEvent -ListLog * | % { Get-WinEvent -LogName $_.logname -MaxEvents 10 }

Open in new window

Parity123Author Commented:
I am trying to get the time difference between the oldest event and the newest event and convert this into minutes, Could you please give me a hand.

$timedifference-in-minutes = newest event - oldest event
This really should be asked as a new question, however...

Each event should have a "timecreated" property, which is a datetime object.  You just need to subtract the old from the new, which will give you a timespan object, which has a "totalminutes" property.  Quick example:
$e = Get-WinEvent -LogName "application" -MaxEvents 10
($e[0].TimeCreated - $e[-1].TimeCreated).TotalMinutes

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Benjamin VoglarIT ProCommented:
$systemlog = Get-EventLog -LogName System -Newest 10 
$applog = Get-EventLog -LogName Application -Newest 10

$all = $systemlog + $applog

$all | Sort-Object TimeGenerated -Descending | 
ft Index,Timegenerated,EntryType,Source,InstanceID,Message -AutoSize

Open in new window

Parity123Author Commented:
When I am dividing current size / totalminutes I get a value output as 2.134523e-05 instead how do I get as 0.000000214 etc.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.