Monday 31 December 2018

Programmatically read "Turn off hard disk after" value in Windows


I'm looking for a way to programmatically read the current setting of the "Turn off hard disk after" value, located in the advanced power options in Windows 7 and later.


I'm aware of the


powercfg -change disk-timeout-ac xx


and


powercfg -change disk-timeout-dc xx


commands to set this value. However, there does not seem to be an equivalent command to read the currently set value. Also, I couldn't find a registry setting for this.



Answer



Since you're asking on Super User instead of Stack Overflow, I'll focus on the scriptable (Registry) way of doing this.


The power settings are all accessible in the Registry under this key:


HKLM\CurrentControlSet\Control\Power

The User\PowerSchemes subkey has an entry called ActivePowerScheme which - as you might guess - contains the ID of the current power plan. All IDs relating to power management are GUIDs, but my quick surveys show that the default ones are the same on all machines. The subkeys of PowerSchemes are, well, power schemes. 381b4222-f694-41f0-9685-ff5bb260df2e, for instance, is Balanced (recommended).


power options


The subkeys of the power scheme keys are categories as they appear in the advanced power options. These are defined in the PowerSettings subkey of Power. For instance, 0012ee47-9041-4b5d-9b77-535fba8b1442 is Hard disk as seen in the above image. The subkeys of those are the individual settings. 6738e2c4-e8a5-4a42-b16a-e040e769756e is Turn off hard disk after. Those keys have a DefaultPowerSchemeValues subkey, which has a subkey for every standard power plan. Those keys have an AcSettingIndex and a DcSettingIndex entry which are exactly what you think. The units used vary from setting to setting; this one uses seconds. So, you can see that the default hard drive power-off timeout for the Balanced power plan is 1200 seconds (20 minutes) when plugged in and 600 seconds (10 minutes) on battery.


registry defaults


Now back to the User\PowerSchemes key. Since the user might not have ever changed the settings, there's no guarantee that the key will have both AC and DC setting entries, the setting subkey, or even the category subkey. If the value you're looking for isn't there, you'll need to check the default value for the current power plan back in PowerSettings\DefaultPowerSchemeValues.


Note that while everybody can read these keys and values, not even Administrators can write. That's because power settings are supposed to be managed through the Power service, which runs as SYSTEM.




Developers of compiled (C++) applications should use the documented interface for power management. Call GetCurrentPowerPolicies, get the user part of the POWER_POLICY structure, and look at the SpindownTimeoutAc and SpindownTimeoutDc values.


No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...