如何获取高级电源设置中Sleep After/Hibernate After的超时数值
Hey there, I totally get your frustration—trying to pull those sleep/hibernate timeouts when your company uses a centralized tool to configure them can be tricky when the usual methods fall flat. Let's break down reliable ways to get these values, whether you're using scripts or code.
Why Your Previous Methods Didn't Work
First, let's quickly cover why you hit roadblocks:
- CMD's
powercfg /query: Spits out tons of raw data, making it hard to isolate the exact timeout values you need. You'd have to filter by specific GUIDs, which isn't user-friendly. - Registry entries: The binary blobs you found are how Windows stores power settings under the hood, but parsing them manually is error-prone. Worse, corporate-managed settings might not even live in those standard registry paths—they could be pushed via Group Policy or MDM, which bypasses local registry entries.
Solution 1: Use PowerShell for Quick Retrieval
PowerShell's CIM (Common Information Model) cmdlets let you directly pull clean, usable values from Windows' power settings store—this works even for settings configured by corporate tools.
Get Sleep Timeout (AC Power)
Get-CimInstance -ClassName Win32_PowerSetting -Filter "ElementName='Sleep after'" | Select-Object InstanceID, ElementName, CurrentValue
The CurrentValue is in seconds—divide by 60 to get minutes if needed.
Get Hibernate Timeout (AC Power)
Get-CimInstance -ClassName Win32_PowerSetting -Filter "ElementName='Hibernate after'" | Select-Object InstanceID, ElementName, CurrentValue
If you need battery-powered settings, you'll need to check the active power plan's settings. Here's how:
# Get active power plan GUID $activePlan = Get-CimInstance -ClassName Win32_PowerPlan -Filter "IsActive='True'" # Get sleep timeout for battery Get-CimInstance -ClassName Win32_PowerSettingDataIndex -Filter "InstanceID like '%$($activePlan.InstanceID.Split('\')[-1])%' AND ElementName='Sleep after'" | Select-Object ElementName, CurrentValue
Solution 2: Retrieve Values in Code (C# Example)
If you need to integrate this into an application, use WMI (Windows Management Instrumentation) to call the same CIM interfaces PowerShell uses.
First, add a reference to System.Management in your project, then use this code:
using System; using System.Management; class PowerTimeoutReader { static void Main() { // Get Sleep Timeout (AC) var sleepSearcher = new ManagementObjectSearcher( "SELECT * FROM Win32_PowerSetting WHERE ElementName='Sleep after'" ); foreach (ManagementObject setting in sleepSearcher.Get()) { int seconds = Convert.ToInt32(setting["CurrentValue"]); Console.WriteLine($"Sleep Timeout (AC): {seconds / 60} minutes"); } // Get Hibernate Timeout (AC) var hibernateSearcher = new ManagementObjectSearcher( "SELECT * FROM Win32_PowerSetting WHERE ElementName='Hibernate after'" ); foreach (ManagementObject setting in hibernateSearcher.Get()) { int seconds = Convert.ToInt32(setting["CurrentValue"]); Console.WriteLine($"Hibernate Timeout (AC): {seconds / 60} minutes"); } } }
This code will pull the active, corporate-configured values directly from the system, so you don't have to hardcode anything.
Key Notes
- These methods work for settings pushed via Group Policy, Intune, or other MDM tools—they read the effective settings that Windows is actually using, not just local overrides.
- If you need to check both AC and battery modes, expand the code/scripts to query the
Win32_PowerSettingDataIndexclass, which ties settings to specific power plans.
内容的提问来源于stack exchange,提问作者Asaf Shazar

