DHB-Scripting Community Forum
»
Technical Advice
»
Scripting
»
Line Up Your Windows Power and Sleep Settings with PowerShell and WMI
Rank: Advanced Member
Groups: Registered
Joined: 7/1/2018(UTC) Posts: 64  Thanks: 1 times Was thanked: 6 time(s) in 6 post(s)
|
LINE UP YOUR WINDOWS POWER and SLEEP SETTINGS with POWERSHELL and WMI Windows Power Settings have always been a little bit confusing. I have found many articles on how to use PowerCfg.exe which returns the setting values (on a single computer) in ugly hex. Unless I'm totally missing something, the same information is available in WMI which can easily be accessed remotely by using PowerShell. Throw in a couple custom objects, slice, dice, and tag the data, then it really starts to make sense. In a mission to baseline the settings for a new client, I came up with this. 3 relatively simple WMI queries. 
Create an Object to store the results: Code:
$PowerPlanObj = [PSCustomObject]@{
InstanceId = $null
ElementName = $null
Settings = New-Object System.Collections.Generic.List[PSObject]
}
WMI Query #1 - Get the Active Power Plan (nothing at all new here). Code:
try {
# Get the Active Power Plan
$ActivePlan = gwmi -Namespace "root\cimv2\power" -Query "select ElementName,InstanceID from Win32_Powerplan where IsActive=True" -ErrorAction Stop
write-host ("Active Plan: " + $ActivePlan.InstanceId + " - " + $ActivePlan.ElementName)
# Set the Active Plan info in the object
$PowerPlanObj.InstanceId = $ActivePlan.InstanceId
$PowerPlanObj.ElementName = $ActivePlan.ElementName
} catch {
write-host "Error Querying root\cimv2\power Win32_Powerplan - make sure you have an elevated window"
write-host $_.ToString()
exit
}
WMI Query #2 - Get the readable names for the settings. Turn the results into a hashtable. Code:
# Get the readable names for the settings
try {
$SettingNames = gwmi -Namespace "root\cimv2\power" -Query "select ElementName,InstanceID from Win32_PowerSetting" -ErrorAction Stop
} catch {
write-host "Error Querying root\cimv2\power Win32_PowerSetting"
write-host $_.ToString()
exit
}
# Loop through the setting names and add them to a hashtable by setting id (use this to get the friendly name)
$SettingHash = @{}
foreach ($i in $SettingNames) {
# Turn this Microsoft:PowerSetting\{29f6c1db-86da-48c5-9fdb-f2b67b1f44da}
# into {29f6c1db-86da-48c5-9fdb-f2b67b1f44da} for the key
$SettingId = $i.InstanceId.Split("\")[-1]
# Add it to the hash
$SettingHash.Add($SettingId,$i)
# Example: $i.ElementName = Sleep after
}
WMI Query #3 - Get the settings for the active plan. Code:
# Change Microsoft:PowerPlan\{381b4222-f694-41f0-9685-ff5bb260df2e}
# to 381b4222-f694-41f0-9685-ff5bb260df2e for the query
$ActivePlanId = $PowerPlanObj.InstanceId.Split("\")[-1].Replace("{","").Replace("}","")
try {
$Settings = gwmi -Namespace "root\cimv2\power" -Query "select InstanceId,SettingIndexValue from Win32_PowerSettingDataIndex Where InstanceId Like '%$ActivePlanId%'" -ErrorAction Stop
} catch {
write-host "Error Querying root\cimv2\power Win32_PowerSettingDataIndex"
write-host $_.ToString()
exit
}
Build the Object for the settings with the relevant info, tag them, and output.
Code:
foreach ($i in $Settings) {
$SettingObj = [PSCustomObject]@{
PlanId = $PowerPlanObj.InstanceId
PlanName = $PowerPlanObj.ElementName
SettingId = $i.InstanceId.Split("\")[-1]
SettingValue = $i.SettingIndexValue
SettingType = $null
SettingName = $SettingHash.($i.InstanceId.Split("\")[-1]).ElementName
}
# Tag the settings AC or DC
if ($i.InstanceId -match "AC") {
$SettingObj.SettingType = "AC"
}
if ($i.InstanceId -match "DC") {
$SettingObj.SettingType = "DC"
}
# Add the setting to the list
$PowerPlanObj.Settings.Add($SettingObj)
}
# Output the settings
$PowerPlanObj.Settings | Out-GridView
Super easy to find settings by name from the active plan that you care about:
Feel free to comment and follow me on Twitter Thanks - Dustin Edited by user Thursday, October 10, 2019 8:06:33 AM(UTC)
| Reason: Not specified
|
 1 user thanked Dustin Higgins for this useful post.
|
|
|
DHB-Scripting Community Forum
»
Technical Advice
»
Scripting
»
Line Up Your Windows Power and Sleep Settings with PowerShell and WMI
Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.