Forum Discussion
ZoeyHall
Aug 08, 2024Copper Contributor
Using Get-WinEvent to Retrieve Events within a Specific Time Period
Does anyone know if it is possible to define a time range when using Get-WinEvent in PowerShell? It appears to work when specifying StartTime, but encounters issues when setting both StartTime and En...
Ermiass
Aug 17, 2024Iron Contributor
Example Script
# Define the start and end times for the event filtering
$startTime = Get-Date "2023-10-01 00:00:00"
$endTime = Get-Date "2023-10-31 23:59:59"
# Define the event log to search (e.g., 'System' or 'Application')
$logName = "System"
# Create a filter hashtable with the specified time range
$filterHashtable = @{
LogName = $logName
StartTime = $startTime
EndTime = $endTime
}
# Get the events using the filter
$events = Get-WinEvent -FilterHashtable $filterHashtable
# Display the events
$events | Format-Table -Property TimeCreated, Id, Message -AutoSize
# Define the start and end times for the event filtering
$startTime = Get-Date "2023-10-01 00:00:00"
$endTime = Get-Date "2023-10-31 23:59:59"
# Define the event log to search (e.g., 'System' or 'Application')
$logName = "System"
# Create a filter hashtable with the specified time range
$filterHashtable = @{
LogName = $logName
StartTime = $startTime
EndTime = $endTime
}
# Get the events using the filter
$events = Get-WinEvent -FilterHashtable $filterHashtable
# Display the events
$events | Format-Table -Property TimeCreated, Id, Message -AutoSize