PowerShell Event Logs – Filtering by Specific Time Range

datepowershellquerytimewindows-event-log

How can I query windows server events between two times of any day?
I have tried with PowerShell…

Get-EventLog -Logname xxxx -After 04:00:00 -Before 04:00:30

…but just returns today's events

Best Answer

To find events between two times at any day we'll want to use a regex. Example code to find any event that happened between 04:00:00 and 04:29:59 of any day in the System log:

Get-EventLog -LogName System | ?{$_.TimeGenerated -match "04:[0-2][0-9]:[0-5][0-9]"}