ken82m

Set On Lid Close Power Option

11 posts in this topic

#1 ·  Posted (edited)

I figured this out for someone on the support forum.

I don't know if anyone else will find a use for it but I thought I'd throw it up anyway.

It quite simple sets the value in:

Control Panel>Power Options>Advanced Tab>When I Close The Lid of My Portable Computer

You have three options: Do Nothing , Standby , Hibernate

Enjoy.

Kenny

#cs --------------------------------------------------------------------------------------
 AutoIt Version: 3.2.13.11 (beta)
 Author:         Kenneth P. Morrissey (Kenny782)

 Script Function:
    This function will allow you to control the action when you close the lid on
    your laptop. The corresponding combobox is in Power Options>Advanced Tab.
    
 Syntax:
    _SetLidPowerAction ( $sAction )
    
 Parameters:
    $sAction:       The action you would like to set on closing the laptops lid.
                        00 - Do Nothing
                        02 - Standby
                        03 - Hibernate

 Returns:
                     Success: Returns 1
                     Failure: Returns 0
                        Sets @error to 1 - Invalid Parameter
                        Sets @error to 2 - Could not read or verify current settings
                        Sets @error to 2 - Error writing new registry value

 Notes:              I have only tested this on XP. The values or position could be
                     different on other OS's. I added a verification to make sure the
                     the current values match one of the three settings. But you should
                     still backup the following key before trying it on say Vista
                     "HKCU\Control Panel\PowerCfg\GlobalPowerPolicy"

 Example:
                     _SetLidPowerAction(02)
#ce --------------------------------------------------------------------------------------
 
Func _SetLidPowerAction($sAction)
    If $sAction <> 00 AND $sAction <> 02 AND $sAction <> 03 Then 
        SetError(1)
        Return 0
    EndIf
    $Key = RegRead("HKCU\Control Panel\PowerCfg\GlobalPowerPolicy", "Policies")
    If $Key = "" Then
        SetError(2)
        Return 0
    EndIf
    $Key = StringReplace($Key, 106, $sAction)
    $Key = StringReplace($Key, 130, $sAction)
    If $sAction <> 00 Then
        $Key = StringReplace($Key, 127, 00)
        $Key = StringReplace($Key, 151, 00)
    Else
        $Key = StringReplace($Key, 127, 80)
        $Key = StringReplace($Key, 151, 80)
    EndIf
    If $Key <> "" Then
        If RegWrite("HKCU\Control Panel\PowerCfg\GlobalPowerPolicy", "Policies", "REG_BINARY", $Key) = 1 Then
            RunWait(@ComSpec & " /c rundll32.exe powrprof.dll,LoadCurrentPwrScheme", @SystemDir, @SW_HIDE)
            Return 1
        EndIf
    EndIf
    SetError(3)
    Return 0
EndFunc
Edited by ken82m

My Contributions _StringMultiReplace PC Builders Console - Secure PDF Creator - Cisco VPN Installer MS DNS Server Backup Script - MS DHCP Backup Script IT Admin Console - Toggle Admin Mode - MyMovies-Add Discs Script - IT Help Desk and System Information Tool - Set On Lid Close Power Option - Streaming Media Server & Website "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Share this post


Link to post
Share on other sites



Hi ken82m,

I want to pull the sleep time value and power button action using registry. Is it possible?

Do you have any idea?

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

ken82m was: Last Active Sep 21 2011 07:56 AM :) Edited by funkey

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
Share on other sites

Anyone got a solution to shutdown the computer when the lid is closed? Sorry for the awesome bump. 

Share this post


Link to post
Share on other sites

you could add the $action value "04", and when passed in,  run "shutdown -p" (Turn off the local computer with no time-out or warning)

Share this post


Link to post
Share on other sites

you could add the $action value "04", and when passed in,  run "shutdown -p" (Turn off the local computer with no time-out or warning)

 

Thanks for your response.

I'm running Windows 7 and this doesn't work. The power option is still set to "Sleep" and not "Shutdown" when lid is closed. I've tried powercfg commands and still no results. 

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

I'm not sure about doing it through the registry, but you can set it through powercfg commands.

The pre-configured schemes have the following GUIDs:

Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e  (Balanced)
Power Scheme GUID: 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c  (High performance)
Power Scheme GUID: a1841308-3541-4fab-bc81-f71556f20b4a  (Power saver)

I'll use the Balanced scheme for my examples, but you would use the GUID provided by:

powercfg -GETACTIVESCHEME

You can find the GUIDs for subgroups and power settings as well as the index values for each power setting by running a query command with your scheme GUID:

powercfg -Q 381b4222-f694-41f0-9685-ff5bb260df2e
Looking through the output, you will discover that the subgroup GUID you want is: 
Subgroup GUID: 4f971e89-eebd-4455-a8de-9e59040e7347  (Power buttons and lid)

and the power setting:

Power Setting GUID: 5ca83367-6e45-459f-a27b-476b1d01c936  (Lid close action)

with index options:

Possible Setting Index: 000
Possible Setting Friendly Name: Do nothing
Possible Setting Index: 001
Possible Setting Friendly Name: Sleep
Possible Setting Index: 002
Possible Setting Friendly Name: Hibernate
Possible Setting Index: 003
Possible Setting Friendly Name: Shut down

So in order to configure your system to Shut down when the lid is closed, you would run:

powercfg -SETACVALUEINDEX 381b4222-f694-41f0-9685-ff5bb260df2e 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 3
powercfg -SETDCVALUEINDEX 381b4222-f694-41f0-9685-ff5bb260df2e 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 3

AC for the "Plugged In" action and DC for the "On Battery" action.

Edited by zorphnog
1 person likes this

Share this post


Link to post
Share on other sites

I'm not sure about doing it through the registry, but you can set it through powercfg commands.

The pre-configured schemes have the following GUIDs:

Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e  (Balanced)
Power Scheme GUID: 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c  (High performance)
Power Scheme GUID: a1841308-3541-4fab-bc81-f71556f20b4a  (Power saver)

I'll use the Balanced scheme for my examples, but you would use the GUID provided by:

powercfg -GETACTIVESCHEME

You can find the GUIDs for subgroups and power settings as well as the index values for each power setting by running a query command with your scheme GUID:

powercfg -Q 381b4222-f694-41f0-9685-ff5bb260df2e
Looking through the output, you will discover that the subgroup GUID you want is: 
Subgroup GUID: 4f971e89-eebd-4455-a8de-9e59040e7347  (Power buttons and lid)

and the power setting:

Power Setting GUID: 5ca83367-6e45-459f-a27b-476b1d01c936  (Lid close action)

with index options:

Possible Setting Index: 000
Possible Setting Friendly Name: Do nothing
Possible Setting Index: 001
Possible Setting Friendly Name: Sleep
Possible Setting Index: 002
Possible Setting Friendly Name: Hibernate
Possible Setting Index: 003
Possible Setting Friendly Name: Shut down

So in order to configure your system to Shut down when the lid is closed, you would run:

powercfg -SETACVALUEINDEX 381b4222-f694-41f0-9685-ff5bb260df2e 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 3
powercfg -SETDCVALUEINDEX 381b4222-f694-41f0-9685-ff5bb260df2e 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 3

AC for the "Plugged In" action and DC for the "On Battery" action.

 

Woho! It worked after a reboot. Thanks for the help! 

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

I'm not sure about doing it through the registry, but you can set it through powercfg commands.

 

The pre-configured schemes have the following GUIDs:

Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e  (Balanced)
Power Scheme GUID: 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c  (High performance)
Power Scheme GUID: a1841308-3541-4fab-bc81-f71556f20b4a  (Power saver)
I'll use the Balanced scheme for my examples, but you would use the GUID provided by:

powercfg -GETACTIVESCHEME
You can find the GUIDs for subgroups and power settings as well as the index values for each power setting by running a query command with your scheme GUID:

powercfg -Q 381b4222-f694-41f0-9685-ff5bb260df2e
Looking through the output, you will discover that the subgroup GUID you want is: 

Subgroup GUID: 4f971e89-eebd-4455-a8de-9e59040e7347  (Power buttons and lid)
and the power setting:

Power Setting GUID: 5ca83367-6e45-459f-a27b-476b1d01c936  (Lid close action)
with index options:

Possible Setting Index: 000
Possible Setting Friendly Name: Do nothing
Possible Setting Index: 001
Possible Setting Friendly Name: Sleep
Possible Setting Index: 002
Possible Setting Friendly Name: Hibernate
Possible Setting Index: 003
Possible Setting Friendly Name: Shut down

So in order to configure your system to Shut down when the lid is closed, you would run:

powercfg -SETACVALUEINDEX 381b4222-f694-41f0-9685-ff5bb260df2e 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 3
powercfg -SETDCVALUEINDEX 381b4222-f694-41f0-9685-ff5bb260df2e 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 3
AC for the "Plugged In" action and DC for the "On Battery" action.

 

 

Hi, thanks for all the hard work you put into that.

 

I've made a vbs script which basically toggles between Sleep and DoNothing.

 

When I run it, in control panel I can see that the lid close action changes, however when I actually close the lid, there is no change in behavior .

 

I checked and I am using the same 'Balanced' plan, same GUID etc.

 

Any suggestions on what could be preventing the system from obeying it's instructions?? It's really strange that I can see the change in ControlPanel>PowerOptions, but it doesn't work...

 

Video demo here, not sure how to embed... https://www.youtube.com/watch?v=N1yjiTMgnzk&feature=youtu.be

The vbs script basically checks what values are in a .bat file, reverses them and runs it

 

ToggleLidAction.vbs

Const ForAppending = 8
Const ForReading = 1

Dim outPutFile

Dim objFSO, objFile, objOutFile, strLine
dim sleepLine1, sleepLine2, doNothingLine1, doNothingLine2
Dim LidState

sleepLine1 = "powercfg -SETACVALUEINDEX 381b4222-f694-41f0-9685-ff5bb260df2e 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 1"
sleepLine2 = "powercfg -SETDCVALUEINDEX 381b4222-f694-41f0-9685-ff5bb260df2e 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 1"
doNothingLine1 = "powercfg -SETACVALUEINDEX 381b4222-f694-41f0-9685-ff5bb260df2e 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 0"
doNothingLine2 = "powercfg -SETDCVALUEINDEX 381b4222-f694-41f0-9685-ff5bb260df2e 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 0"


LidState = ""

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("ToggleLidAction.bat", ForReading)

Do Until objFile.AtEndOfStream
    strLine = objFile.ReadLine
    strDoNothing = Instr(strLine,"powercfg -SETDCVALUEINDEX 381b4222-f694-41f0-9685-ff5bb260df2e 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 0")
    strSleep = Instr(strLine,"powercfg -SETDCVALUEINDEX 381b4222-f694-41f0-9685-ff5bb260df2e 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 1")
Loop
objFile.Close

Set obj = CreateObject("Scripting.FileSystemObject")
obj.DeleteFile("ToggleLidAction.bat")

If strDoNothing Then
    outPutFile = "ToggleLidAction.bat"
    Set objOutFile = objFSO.CreateTextFile(outPutFile,ForAppending)
    objOutFile.WriteLine ""& sleepLine1 &""
    objOutFile.WriteLine ""& sleepLine2 &""
    Msgbox ("The System will now Sleep when the lid is closed.")
else if strSleep Then
    outPutFile = "ToggleLidAction.bat"
    Set objOutFile = objFSO.CreateTextFile(outPutFile,ForAppending)
    objOutFile.WriteLine ""& doNothingLine1 &""
    objOutFile.WriteLine ""& doNothingLine2 &""
    Msgbox ("The System will now continue running when the lid is closed.")
end if
end if
objOutFile.Close

Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "ToggleLidAction.bat C:\WINDOWS\system32\cmd.exe", 0

ToggleLidAction.bat

powercfg -SETACVALUEINDEX 381b4222-f694-41f0-9685-ff5bb260df2e 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 0
powercfg -SETDCVALUEINDEX 381b4222-f694-41f0-9685-ff5bb260df2e 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 0

or

powercfg -SETACVALUEINDEX 381b4222-f694-41f0-9685-ff5bb260df2e 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 1
powercfg -SETDCVALUEINDEX 381b4222-f694-41f0-9685-ff5bb260df2e 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 1

Though I don't think it's a problem with the code, because as I said, I can see the change in Control Panel.

 

Thanks for reading,

 

Any suggestions greatly appreciated.

#### EDIT Found the solution ####

Settings made via the command line must be followed with "powercfg -setactive GUID"

ie for 'Balanced' "powercfg -setactive 381b4222-f694-41f0-9685-ff5bb260df2e"

So I have my desktop icon that toggles between Sleep/DoNothing when the lid is closed. That's what I've been looking for for years!!

If anybody wants me to post it PM or post here...

Edited by HendrikWitbooi

Share this post


Link to post
Share on other sites

Hi

Could youn post it? Thanks!

Share this post


Link to post
Share on other sites

@wobber His reply was almost 2 years old and he was only active for 3 days after he posting the reply! Please make a new topic :)


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now