"It's not what you can do, it's what you can get done."

Friday, June 18, 2010

Script to temporarily keep windows 7 from sleeping when closing the lid.

Normally I want my laptop (Windows 7 on a Dell D630) to sleep and/or hibernate or hybrid sleep when I close the lid. Occasionally though I want the exact opposite. I tried Don't Sleep and Insomnia, which didn't work when closing the lid. (I tried launching them elevated as well.) Every blog post I found seemed to be either explaining how to modify a power scheme, or a post on new "-energy" report.




Time to check out powercfg... Maybe I just don't know how to navigate Technet, but the only reference I can find is on the Vista version.



"powercfg /?" seems to be the best bet.


c:\>powercfg -l


Existing Power Schemes (* Active)
-----------------------------------
Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (Balanced)
Power Scheme GUID: 500d0628-b98d-417b-b7e9-fa400ae0653c (Mine) *
Power Scheme GUID: 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c (High performance)
Power Scheme GUID: a1841308-3541-4fab-bc81-f71556f20b4a (Power saver)


In XP you could reference power configs via text labels. Apparently now you have to use GUID. Thanks Microsoft. Thicrosoft.


I already have a scheme, the GUI won't let me duplicate it, so:
c:\>powercfg -DUPLICATESCHEME 500d0628-b98d-417b-b7e9-fa400ae0653c
Power Scheme GUID: 80e706a3-bdaf-44c4-8b3b-29eecce46464 (Mine)
c:\>powercfg -l


Existing Power Schemes (* Active)
-----------------------------------
Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (Balanced)
Power Scheme GUID: 500d0628-b98d-417b-b7e9-fa400ae0653c (Mine) *
Power Scheme GUID: 80e706a3-bdaf-44c4-8b3b-29eecce46464 (Mine)
Power Scheme GUID: 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c (High performance)
Power Scheme GUID: a1841308-3541-4fab-bc81-f71556f20b4a (Power saver)


c:\>powercfg -CHANGENAME 80e706a3-bdaf-44c4-8b3b-29eecce46464 MineNolidSleep


At this point you could modify settings using the GUI, but since we're already in commandline land.


This is just getting ridiculous. Time to make things a bit easier:
c:\>set PWRNormal=500d0628-b98d-417b-b7e9-fa400ae0653c
c:\>set PWRNoSleepLid=80e706a3-bdaf-44c4-8b3b-29eecce46464


So now you need to query the scheme to get the GUID of the item you want:
c:\>powercfg -q %PWRNoSleepLid%


This will dump a huge list of options. I copied it to a texteditor and found what I wanted:
Subgroup GUID: 4f971e89-eebd-4455-a8de-9e59040e7347 (Power buttons and lid)
Power Setting GUID: 5ca83367-6e45-459f-a27b-476b1d01c936 (Lid close action)


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
Current AC Power Setting Index: 0x00000001
Current DC Power Setting Index: 0x00000001
So to tell it to do nothing when shutting the lid, we want setting index 0.
c:\>set SUBGUID=4f971e89-eebd-4455-a8de-9e59040e7347
c:\>set SETGUID=5ca83367-6e45-459f-a27b-476b1d01c936
I want to set it for both AC and DC. (Plugged in and on battery.)
c:\>powercfg -SETACVALUEINDEX %SUBGUID% %SETGUID% 0
c:\>powercfg -SETDCVALUEINDEX %SUBGUID% %SETGUID% 0


So now we can simply choose which profile we want:
c:\>powercfg -s %PWRNormal%
c:\>powercfg -s %PWRNoSleepLid%

Now, take the relative pieces and put 'em in a batch file or two, voila!