locked
How to code powershell to switch power mode? RRS feed

  • Question

  • Referring to following PowerShell's coding, I would like to know on what wrong it is not working under Window 7 Home 64 bit

    Does anyone have any suggestions?
    Thanks in advance for any suggestions

    Try {
        $HighPerf = powercfg -l | %{if($_.contains("Power Saver")) {$_.split()[3]}}
        $CurrPlan = $(powercfg -getactivescheme).split()[3]
        if ($CurrPlan -ne $HighPerf) {powercfg -setactive $HighPerf}
    }

    Catch {
        Write-Warning -Message "Unable to set power plan to Power Saver"
    }


    Thanks in advance for any suggestions

    • Moved by Bill_Stewart Tuesday, December 30, 2014 2:45 AM Off-topic help vampire question
    Wednesday, December 3, 2014 3:43 PM

Answers

  • Under WIndows 7 this is where the value is:

     $(powercfg -getactivescheme).split()[5]

    Run it without the subscript and count:

    PS C:\scripts> $(powercfg -getactivescheme).split()
    Power
    Scheme
    GUID:
    381b4222-f694-41f0-9685-ff5bb260df2e

    (Balanced)
    PS C:\scripts>


    ¯\_(ツ)_/¯

    • Proposed as answer by jrv Wednesday, December 3, 2014 5:39 PM
    • Marked as answer by oemMicrosoft Wednesday, December 3, 2014 10:21 PM
    • Unmarked as answer by oemMicrosoft Wednesday, December 3, 2014 10:31 PM
    • Marked as answer by Just Karl Friday, May 1, 2015 9:47 PM
    Wednesday, December 3, 2014 4:39 PM

All replies

  • When you say it doesn't work, what error do you get? That is, if you don't catch the error.
    Wednesday, December 3, 2014 3:49 PM
  • Hi oem,

    I don't have a home edition at hand, but many administrative features are not available in home editions. Try opening a powershell console on such a device and run

    powercfg -l

    Do you get the data it should show? Does it throw an error?
    If it throws an error, what does it say? (Screenshots are great)

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Wednesday, December 3, 2014 3:58 PM
  • Based on powercfg -l, it shows all modes for power and balance is active one.

    Could anyone please tell me how to run those scripts under powershell?

    I try ".\PS.ps1" command, it does not seem working, the error is in Chinese, and it is hard to translate into English. but I try other powershell scripts with no problem.

    Could anyone try to run those scripts? Does it work? If it does not work,

    Does anyone have any suggestions on how to code powershell scripts to switch Power mode under Win7 (64 bit), I think those scripts should work on any version of Windows 7 64 bit, so never mind you don't have Home edition in your hand :>

    Thanks, to everyone very much for any suggestions :>


    Thanks in advance for any suggestions


    • Edited by oemMicrosoft Wednesday, December 3, 2014 4:40 PM
    Wednesday, December 3, 2014 4:25 PM
  • Under WIndows 7 this is where the value is:

     $(powercfg -getactivescheme).split()[5]

    Run it without the subscript and count:

    PS C:\scripts> $(powercfg -getactivescheme).split()
    Power
    Scheme
    GUID:
    381b4222-f694-41f0-9685-ff5bb260df2e

    (Balanced)
    PS C:\scripts>


    ¯\_(ツ)_/¯

    • Proposed as answer by jrv Wednesday, December 3, 2014 5:39 PM
    • Marked as answer by oemMicrosoft Wednesday, December 3, 2014 10:21 PM
    • Unmarked as answer by oemMicrosoft Wednesday, December 3, 2014 10:31 PM
    • Marked as answer by Just Karl Friday, May 1, 2015 9:47 PM
    Wednesday, December 3, 2014 4:39 PM
  • OEM - we are not going down this black hole with you yet once again. Your original question has been answered.


    ¯\_(ツ)_/¯

    Wednesday, December 3, 2014 5:39 PM
  • Under WIndows 7 this is where the value is:

     $(powercfg -getactivescheme).split()[5]

    Run it without the subscript and count:

    PS C:\scripts> $(powercfg -getactivescheme).split()
    Power
    Scheme
    GUID:
    381b4222-f694-41f0-9685-ff5bb260df2e

    (Balanced)
    PS C:\scripts>


    When I run PS.ps1 under powershell as administrator, it shows no error, but when I check the current power mode under control panel, the mode is still balance instead of power saver.

    Furthermore, would it be possible to run scripts each line as a time, so I can see what is going wrong at each line's variables.

    Does anyone have any suggestions on what wrong it is?
    Thanks, to everyone very much for any suggestions :>

    Try {
        $PowSav = powercfg -l | %{if($_.contains("省電")) {$_.split()[3]}}
        $CurrPlan = $(powercfg -getactivescheme).split()[3]
        if ($CurrPlan -ne $PowSav) {powercfg -setactive $PowSav}
    }

    Catch {
        Write-Warning -Message "Unable to set power plan to 省電"
    }


    Thanks in advance for any suggestions



    • Edited by oemMicrosoft Wednesday, December 3, 2014 10:36 PM
    Wednesday, December 3, 2014 10:33 PM
  • Please read the help for PowerCfg.  YTuo are not setting it to ahything.

     powercfg  /s /?

    You have to know the GUID of the target scheme in advance.

    POWERCFG /L

    shows you all available schemes.

    POWERCFG /?

    tells you how to run the utility.

    None of this has anything to do with scripting,  It is just basic use of a Windows utility. 


    ¯\_(ツ)_/¯

    Wednesday, December 3, 2014 10:40 PM
  • Utilities are usually handled by the OS forum for your OS. E.G. Vista utilities in the Vista OS forum.


    ¯\_(ツ)_/¯

    Thursday, December 4, 2014 3:03 AM
  • Thanks, to everyone very much for any suggestions :>

    Thanks in advance for any suggestions


    Thursday, December 4, 2014 3:05 AM