-
Notifications
You must be signed in to change notification settings - Fork 1
/
PowerSettings.ps1
44 lines (37 loc) · 1.67 KB
/
PowerSettings.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# PowerShell script must run as admin
$ErrorActionPreference = "Stop"
function PrintPowerSetting ($setting, $acValue, $dcValue) {
Write-Host ID: $setting.InstanceID
Write-Host Name: $setting.ElementName
Write-Host Description: $setting.Description
Write-Host AC value: $acValue
Write-Host DC value: $dcValue
}
# Get current power plan (Balanced, High performance, Power saver, ..)
$powerplan = Get-WmiObject -Namespace "root\cimv2\power" -Class Win32_powerplan | where {$_.IsActive}
# Doc: https://learn.microsoft.com/en-us/previous-versions/windows/desktop/powerwmiprov/win32-powersettingdataindex
$settings = $powerplan.GetRelated("Win32_PowerSettingDataIndex")
foreach ($settingidx in $settings) {
# will iterate over S0_AC, S0_DC, S1_AC, S1_DC, S2_AC, S2_DC, ...
$acdc = $settingidx.InstanceID.split("\")[2] # determine if AC or DC setting
if ($acdc -eq "AC") {
$acValue = $settingidx.SettingIndexValue
$acID = $settingidx.InstanceID.split("\")[3]
continue # corresponding DC value will come in next iteration
} else {
$dcValue = $settingidx.SettingIndexValue
$dcID = $settingidx.InstanceID.split("\")[3]
}
if ($acID -ne $dcID) { # sanity check
Write-Error "AC vs. DC InstanceID mismatch"
Exit
}
# Doc: https://learn.microsoft.com/en-us/previous-versions/windows/desktop/powerwmiprov/win32-powersetting
$setting = $settingidx.GetRelated("Win32_PowerSetting")
Write-Host # blank line
PrintPowerSetting $setting $acValue $dcValue
# uncomment to modify DC settings to match AC
#$settingidx.SettingIndexValue = $acValue
#[void]$settingidx.Put() # store change
#break
}