Auto-Close Applications & Shutdown Windows When UPS Switches to Battery Mode

Hey B&R Community! :waving_hand:

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 :red_circle: Power lost - UPS on battery (Your trigger!)
9 :green_circle: Power restored
13 :warning: Battery critically low
265 :desktop_computer: Computer shutting down

Step 2: Create Automated Task

Open Windows Task Scheduler and create a new basic task:

  1. Name: “UPS Battery Shutdown”

  2. Trigger:

    • Event Log: Application

    • Source: ADI UPS

    • Event ID: 4

  3. Action: Run batch file “location of your batch file"

  4. 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.exe with your actual app)

  • Waits 30 seconds for proper cleanup

  • Shuts down Windows


Important Notes:

:warning: Test on non-critical system first
:warning: Verify correct Event IDs in Event Viewer
:warning: Set proper file permissions
:warning: 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

3 Likes

thanks for sharing :wink: