Posted On May 22, 2021

PowerShell: Export Event Logs by a Certain Date Range

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Export Event Logs by a Certain Date Range
# User defined variables
$daysLimit=7
$startdate=(get-date).adddays(-$daysLimit)
$computername=$env:computername
$logName='Application'
$filterTypes='Error','Warning'
$logPath="c:\temp\eventlogs-from-$($startdate.tostring('yyyy-MM-dd'))-to-$((get-date).tostring('yyyy-MM-dd')).csv"

# Get the event logs
if(!(test-path $(split-path $logpath -parent))){$null=mkdir $(split-path $logpath -parent)}
$eventLogs = get-eventlog -ComputerName $computername -log $logName -After $startdate -EntryType $filterTypes
$eventLogs | export-csv $logPath

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: Kill a Windows Service Forcefully

# killService.ps1 $serviceName='vmms' function killService($serviceName='Spooler',$restart=$false){ $processId=Get-WmiObject -Class Win32_Service -Filter "Name LIKE '$serviceName'"|Select-Object -ExpandProperty ProcessId if($processId.count…

PowerShell: How To Disable and/or Stop Windows Service

Disable and Stop Using Native Command 'Set-Service' # To disable service as well as stopping…

PowerShell: Update Windows Computers

There's a more updated version available. <# Update-Windows-Computers-v0.11.ps1 # # Purpose: trigger Microsoft Updates on…