How to only run a program when your laptop is plugged in.

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.

  1. Open Task Scheduler.
  2. Click “Create Task…” under Task Scheduler Library.
  3. Name your task and give it a description.
  4. If your application needs admin privileges, make sure to check the box at the bottom, “Run with highest privileges”
  5. Open the Triggers tab at the top. Click “New…” at the bottom.
  6. Change the Begin the task value to “On an event”.
  7. Under “Settings”, change the radio button selection from “Basic” -> “Custom”.
  8. Click “New Event Filter…”.
  9. Disregard the “Filter” tab, and instead change the tab to “XML”.
  10. Check the box at the bottom that states “Edit query manually”.
  11. 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

One thought on “How to only run a program when your laptop is plugged in.”

Leave a Reply

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