Forked from raspi/enable-all-advanced-power-settings.ps1
Last active
November 2, 2021 09:07
-
-
Save qwerty12/1413d9796b4cedb3cd6c72f101a2a2f3 to your computer and use it in GitHub Desktop.
Enable all advanced power settings in Windows.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# List all possible power config GUIDs in Windows | |
# Run: this-script.ps1 | Out-File powercfg.ps1 | |
# Then edit and run powercfg.ps1 | |
# (c) Pekka "raspi" Järvinen 2017 | |
# https://stackoverflow.com/a/22156833 | |
if (-not ([System.Management.Automation.PSTypeName]'PowrProf').Type) | |
{ | |
Add-Type -TypeDefinition @" | |
using System; | |
using System.Runtime.InteropServices; | |
public static class PowrProf | |
{ | |
private static readonly Destructor Finalise = new Destructor(); | |
public static IntPtr subGroupGuidPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Guid))); | |
public static IntPtr powerSettingGuidPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Guid))); | |
[DllImport("powrprof.dll", EntryPoint="PowerReadSettingAttributes", ExactSpelling=true, SetLastError=true)] | |
public static extern UInt32 PowerReadSettingAttributes(IntPtr subGroupGuid, IntPtr powerSettingGuid); | |
public static UInt32 PowerReadSettingAttributes_wrapper(string subGroupGuid, string powerSettingGuid) | |
{ | |
try { | |
Marshal.StructureToPtr(new Guid(subGroupGuid), PowrProf.subGroupGuidPtr, true); | |
Marshal.StructureToPtr(new Guid(powerSettingGuid), PowrProf.powerSettingGuidPtr, true); | |
return PowerReadSettingAttributes(subGroupGuidPtr, powerSettingGuidPtr); | |
} catch { | |
return 0; | |
} | |
} | |
private sealed class Destructor | |
{ | |
~Destructor() | |
{ | |
// https://stackoverflow.com/a/18709110 | |
if (PowrProf.subGroupGuidPtr != IntPtr.Zero) | |
Marshal.FreeHGlobal(subGroupGuidPtr); | |
if (PowrProf.powerSettingGuidPtr != IntPtr.Zero) | |
Marshal.FreeHGlobal(powerSettingGuidPtr); | |
} | |
} | |
} | |
"@ | |
} | |
$powerSettingTable = Get-WmiObject -Namespace root\cimv2\power -Class Win32_PowerSetting | |
$powerSettingInSubgroupTable = Get-WmiObject -Namespace root\cimv2\power -Class Win32_PowerSettingInSubgroup | |
Get-WmiObject -Namespace root\cimv2\power -Class Win32_PowerSettingCapabilities | ForEach-Object { | |
$tmp = $_.ManagedElement | |
$tmp = $tmp.Remove(0, $tmp.LastIndexOf('{') + 1) | |
$tmp = $tmp.Remove($tmp.LastIndexOf('}')) | |
$guid = $tmp | |
$s = ($powerSettingInSubgroupTable | Where-Object PartComponent -Match "$guid") | |
if (!$s) { | |
return | |
} | |
$tmp = $s.GroupComponent | |
$tmp = $tmp.Remove(0, $tmp.LastIndexOf('{') + 1) | |
$tmp = $tmp.Remove($tmp.LastIndexOf('}')) | |
$groupguid = $tmp | |
if (([PowrProf]::PowerReadSettingAttributes_wrapper($groupguid, $guid) -band 1) -ne 1) { | |
return | |
} | |
$s = ($powerSettingTable | Where-Object InstanceID -Match "$guid") | |
$descr = [string]::Format("# {0}", $s.ElementName) | |
$runcfg = [string]::Format("powercfg -attributes {0} {1} -ATTRIB_HIDE", $groupguid, $guid) | |
Write-Output $descr | |
Write-Output $runcfg | |
Write-Output "" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment