Hey B&R Community! ![]()
I want to share a solution that I implemented for automatically shutting down applications on a Windows 10 PC when the UPS switches to battery mode.
The Problem: A customer has a UPS connected to a Windows computer. When the power goes out, they want all applications to close and the system to shut down automatically to prevent data loss.
The Solution: Combine Windows Event Viewer + Task Scheduler to monitor UPS status and trigger an automated shutdown script (batch-file).
Step 1: Monitor UPS Status
The UPS records events in the “Application” section of the “Windows Logs” under the source “ADI UPS”.
Key event IDs:
| Event ID | Meaning |
|---|---|
| 4 | |
| 9 | |
| 13 | |
| 265 |
Step 2: Create Automated Task
Open Windows Task Scheduler and create a new basic task:
-
Name: “UPS Battery Shutdown”
-
Trigger:
-
Event Log:
Application -
Source:
ADI UPS -
Event ID:
4
-
-
Action: Run batch file
“location of your batch file" -
Save and enable
Step 3: Create Batch File
@echo off
:: This batch file is executed when the UPS logs a specific event in the event log.
:: It is triggered by the Windows Task Scheduler.
:: Close the “x”.exe program
taskkill /IM x.exe
if %errorlevel% == 0 (
echo The “x” application was terminated because the UPS is running on battery power.
) else (
echo Error: The program could not be terminated.
)
:: Wait 30 seconds
timeout /t 30
:: Turn off computer
shutdown /s /t 0
What it does:
-
Closes your application (replace
x.exewith your actual app) -
Waits 30 seconds for proper cleanup
-
Shuts down Windows
Important Notes:
Test on non-critical system first
Verify correct Event IDs in Event Viewer
Set proper file permissions
Check Task Scheduler logs for execution confirmation
Of course, you need an application with an auto-save function so that you can close the program without having to confirm or manually save.
That’s how I solved the problem! If you have any questions about this topic, or if you have a better idea about how to implement this kind of behavior, please let me know!
Best regards,
Ben