Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I would like to create a batch file that checks the current system power mode and change it if necessary. I have the following. What am I doing wrong? (it does not work)

@ECHO OFF

SET _CURRENT_POWER_SCHEME = POWERCFG -GETACTIVESCHEME

IF NOT %_CURRENT_POWER_SCHEME% == "Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e  (Balanced)"(
    POWERCFG -SETACTIVE "381b4222-f694-41f0-9685-ff5bb260df2e"

)

Thanks for the help.

share|improve this question
2  
Think changing the power profile requires administrator privileges, have you tried running the batch file as admin? –  HaydnWVN Aug 1 '12 at 8:18
    
@HaydnWVN Yes, even with admin privileges, the file executes and closes the window right-away. Even if I place PAUSE calls inside the IF statement and after it. –  Kovo Aug 1 '12 at 12:04
    
So a PAUSE before the IF still causes it to be closed?! –  HaydnWVN Aug 1 '12 at 13:02
    
No, before the IF, the Pause works as expected. –  Kovo Aug 1 '12 at 15:09

1 Answer 1

up vote 3 down vote accepted

From my experience with batch file scripting, if it exits even when you have a PAUSE that should be getting hit, it means there is an error that causes it to crash. In this case I think it's a few syntax issues, as well as how you are setting the variable. I played around and got this to work:

@ECHO off
FOR /f "delims=" %%a in ('POWERCFG -GETACTIVESCHEME') DO @SET _CURRENT_POWER_SCHEME="%%a"
IF NOT %_CURRENT_POWER_SCHEME%=="Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e  (Balanced)" POWERCFG -SETACTIVE "381b4222-f694-41f0-9685-ff5bb260df2e"

The "For /f..." line is usually the command I use to get the output of another command into a variable. It is normally for parsing through more complex responses, but it handles the -GETACTIVE command in this case. For more info on for loop syntax (or batch syntax in general): http://ss64.com/nt/for_f.html

share|improve this answer
    
That worked like a charm. Thank you very much. –  Kovo Aug 2 '12 at 2:39
1  
No problem. I have had plenty of problems dealing with strings and command outputs using command-line. Sometimes it odd use of quotes (putting quotes around the variable used in polling the command, for example) The originial code, while logically sound, from when I was playing with it, would put the command as a string into the variable instead of the return. –  David M Aug 3 '12 at 18:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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