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

Here’s a tricky one. How do you run a program or exe 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.


It helps me if you share this post

Published 2018-06-29 00:09:10

11 comments on “How to only run a program when your laptop is plugged in”

  1. This is amazing. Not sure why this seems to be such an obscure thing that even StackOverflow doesn’t have an answer. I am using this to start/kill a couple power hungry apps, thanks for writing this post.

  2. This seems like a really useful script for me for some laptop configuration settings, but for some reason I can only make it work when the laptop gets plugged into ac power, it never activates when unplugging from ac, even when changing true to false. It’s also weird that it doesn’t ever run when I put a bogus value like “test” into that spot. Have you seen this problem before?

    1. haven’t needed to use this in a while, not sure

      Have you tried changing AcOnline event to AcOffline?

      ‘false’ should work otherwise though, as long as the other settings are set up correctly, make sure it’s not set up to not run when you aren’t plugged into power and other relevant settings that would affect opposite functionality

    2. Under conditions, there is a checkbox that says “Start the task only if the computer is on AC Power”, disable that and the ‘false’ should work

  3. Thanks for the marvelous posting! I actually enjoyed reading it, you are a great author.I will remember to bookmark your blog and will eventually come back someday.
    I want to encourage you to continue your great
    job, have a nice holiday weekend!

Leave a Reply

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