Windows Server – Monitor Log File Size

Step 1 – Create batch file and save as: MonitorLogFileSize.bat

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""C:\IT-Scripts\MonitorLogFileSize\MonitorLogFileSize.ps1""' -Verb RunAs}"

Step 2 – Create Powershell file and save as: MonitorLogFileSize.ps1

$dir = "D:\MainServer\Logs"
$latest = Get-ChildItem -Path $dir | Sort-Object LastWriteTime -Descending | Select-Object -First 1
$targetFile = "$dir\$latest"
$fileSize = ((Get-ItemProperty $targetfile).length/1MB)
$friendlySize = ([math]::Round($fileSize, 2))
if ($fileSize -gt 200.0)
Send-MailMessage -To [email protected] -From [email protected] -SMTPServer -Subject "Urgent Main Server Alert - $latest is OVERSIZE" -Body "$latest is over $friendlysize MB in path: $dir , please check anomaly behavior!"

*It will check the most recent log file in the path and will monitor its size growth according to your limit (this case: 200MB)

Step 3 – Open Task Scheduler and add a new task

On the task in action -> set to run ‘MonitorLogFileSize.bat every 5 mins

check Run with highest privileges.


*You can decrease the growth -gt 200.0) to something below your current log file – example: -gt 2.0

just to check that you get an email alert on it, then revert it back to your required limit.


Enjoy 🙂