2

I'm trying to make a script that changes Windows power plans depending if the battery is plugged or not. I tried an infinite loop which is constantly checking on the battery status, variable "$batteryStatus" returns a 1 if it is on battery mode and a 2 for AC power mode.

The problem I have is that nothing happens, it remains in the same state. I have read that maybe I can solve this by using eventvwr.msc, but I don't really know how I could adapt this script to events. This is the code on Powershell I have so far:

$equilibradoGUID = '381b4222-f694-41f0-9685-ff5bb260df2e'
$bateriaGUID = '0ed875ed-d70e-4dd1-bc8e-95a18268730f'
$batteryStatus = (Get-WmiObject Win32_Battery).BatteryStatus

while ($True) {
    if ($batteryStatus2 -eq 2){
        Write-Output "AC"
        powercfg -setactive $equilibradoGUID
    }
    else {
        Write-Output "Battery"
        powercfg -setactive $bateriaGUID
    }
} 
  • updated my answer. – somebadhat Apr 11 at 15:28
  • You captured the value of BatteryStatus, once before a while loop, but never capture it again. – Ramhound Apr 11 at 18:22
  • That was it, thanks. Adding the variable to if-else clauses solves the issue. – Alvaromr7 Apr 11 at 19:28
  • You need check if ac/power are/is connected or not? Or if the battery is removed from computer? I not understand.. Sorry.. – It Wasn't Me Apr 17 at 17:02
1

Windows 10 64-bit.

Your approach requires a restart to take effect but if you want to persist this might help - https://stackoverflow.com/questions/7355331

How to change Windows 10 power settings with cmd script that does not require a restart or administrative privileges to take effect:

rem Change Win10 power settings w/o restart or admin privileges
@echo off
setlocal 
:minpower
POWERCFG /CHANGE /MONITOR-TIMEOUT-DC 1
POWERCFG /CHANGE /DISK-TIMEOUT-DC 2
POWERCFG /CHANGE /STANDBY-TIMEOUT-DC 3
POWERCFG /CHANGE /MONITOR-TIMEOUT-AC 1
POWERCFG /CHANGE /DISK-TIMEOUT-AC 2
POWERCFG /CHANGE /STANDBY-TIMEOUT-AC 3
exit /b

:normalpower 
POWERCFG /CHANGE /MONITOR-TIMEOUT-DC 10
POWERCFG /CHANGE /DISK-TIMEOUT-DC 29
POWERCFG /CHANGE /STANDBY-TIMEOUT-DC 30
POWERCFG /CHANGE /MONITOR-TIMEOUT-AC 10
POWERCFG /CHANGE /DISK-TIMEOUT-AC 29
POWERCFG /CHANGE /STANDBY-TIMEOUT-AC 30
exit /b

:maxpower 
POWERCFG /CHANGE /MONITOR-TIMEOUT-DC 1
POWERCFG /CHANGE /DISK-TIMEOUT-DC 601
POWERCFG /CHANGE /STANDBY-TIMEOUT-DC 602
POWERCFG /CHANGE /MONITOR-TIMEOUT-AC 1
POWERCFG /CHANGE /DISK-TIMEOUT-AC 601
POWERCFG /CHANGE /STANDBY-TIMEOUT-AC 602
exit /b
| improve this answer | |
  • Once again we always appreciate the contributions to the Suggested Edit queue, but please do us a small favor. When you are changing a screen shot from a hyperlink to an inline picture, please utilize the same URL that the original author did. If it is already uploaded to i.stack.imgur.com there is no need to change the picture's URL and although we trust you, every time I see that the picture's URL has a different suffix I need to stop and open the original to ensure that it's the same photo. Unfortunately, there are other people who could potentially do something inappropriate. – Run5k Apr 12 at 18:42
  • @run Will do. Your comment should be along the lines of: "It is easier to just change the code to the picture. Why go through the trouble of creating a new image? Inline code looks like:[![enter image description here][1]][1] as opposed to a link:[enter link description here][1] – somebadhat Apr 12 at 20:01
  • I merely wanted to explain the rationale behind my request, good sir. I was focused on the philosophy behind it much more than the logic. – Run5k Apr 12 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.