Here’s a tricky one. How do you run a program ONLY when your Windows laptop is plugged in? Additionally, you’d want it to automatically exit when your laptop was unplugged and you were using battery power. I recently stumbled across this problem when I wanted to run Rainmeter on my laptop when it’s plugged in.
The answer is to use the Task Scheduler.
Here’s what you’ll need to do.
- Open Task Scheduler.
- Click “Create Task…” under Task Scheduler Library.
- Name your task and give it a description.
- If your application needs admin privileges, make sure to check the box at the bottom, “Run with highest privileges”
- Open the Triggers tab at the top. Click “New…” at the bottom.
- Change the Begin the task value to “On an event”.
- Under “Settings”, change the radio button selection from “Basic” -> “Custom”.
- Click “New Event Filter…”.
- Disregard the “Filter” tab, and instead change the tab to “XML”.
- Check the box at the bottom that states “Edit query manually”.
- Paste this XML in:
<QueryList> <Query Id="0" Path="System"> <Select Path="System">*[EventData[Data[@Name='AcOnline']='true']]</Select> </Query> </QueryList>
Click “OK”, and then “OK” again.
Once back on the main page, you may now configure your “Actions” tab to launch your program of choice.
Once finished, proceed to the settings tab and make sure you have these settings:
Congratulations! You can now test it by plugging in your laptop and watching your program launch itself. Unplugging it should quit it instantly.
My Patreon | My Website