Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Possible Duplicate:
Is there a way to execute a program on power events?

Is there a Windows event that I can watch in Task Scheduler that will trigger when the power is plugged in? And an event when the power is unplugged?

I want to start and stop CrashPlan while on battery power. (This has been a 2.5-year-old feature request in CrashPlan and honestly I can't wait for them to add it.)

Others have also asked previously how to disable SuperFetch and Search Indexing on battery power. I think this trick will solve these questions, too.

Is there such a windows event? If not, could there be a program that I can install that detects power changes, which then I can create a Task Scheduler trigger for.

share|improve this question

marked as duplicate by soandos, Ƭᴇcʜιᴇ007, BloodPhilia, ChrisF, Sathya Feb 15 '12 at 12:14

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

By default, Windows does not log an event when you unplug or plug in your computer.

However, with PowerShell you can listen for such a change and write your own events to the log. This script will do that:

New-EventLog -Source BatteryStatusMonitor -LogName Application

Function OnBatteryStatusChange ($NewStatus) {
  If ($NewStatus -eq 1) {
    $EventID = 5001
    $Message = "The computer was unplugged."
  } ElseIf ($NewStatus -eq 2) {
    $EventID = 5002
    $Message = "The computer was plugged in."
  } Else {
    $EventID = 5000
    $Message = "Battery status changed to $NewStatus"
  Write-EventLog -LogName Application -Source BatteryStatusMonitor -EventID $EventID -Message $Message

$Query = "select * from __instancemodificationevent within 3 where targetinstance isa 'win32_battery' and targetinstance.batterystatus <> previousinstance.batterystatus"

Register-WmiEvent -Query $Query -Action {OnBatteryStatusChange $Event.SourceEventArgs.NewEvent.TargetInstance.BatteryStatus} -SourceIdentifier "BatteryStatusChange"

For (;;) {}
share|improve this answer

If you are only interested in supporting Windows Vista and above, register for power events.

Use RegisterPowerSettingNotification with the GUID_ACDC_POWER_SOURCE value.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged .