Powershell - Displaying and changing the power mode in Windows

To support me, you can subscribe to the channel, share and like the videos, disable your ad blocker or make a donation. Thank you!

Hello,

For some time now, I’ve been paying more attention to my energy consumption, and of course that includes the power consumption of my work PC. In normal use, I haven’t seen any big difference in power consumption between normal and performance mode (in normal use), but in energy-saving mode I’ve reduced it by more than 60w (roughly 140w instead of 200w).
I haven’t noticed any slowdown in the power consumption of my PC. I didn’t experience any annoying sluggishness in power-saving mode, but to be on the safe side I switch to performance mode when I’m making and encoding videos or 3D.
. But switching from economy to performance takes too much graphical action (and I’m lazy) whereas a small powershell function launches quickly.

# List power modes
Get-CimInstance -Name root\cimv2\power -Class Win32_PowerPlan | Select-Object -Property ElementName, instanceid, IsActive
# or
PowerCfg.exe -list
# List the power mode currently in use
Get-CimInstance -Name root\cimv2\power -Class Win32_PowerPlan -filter "IsActive = 'true'"
# or
PowerCfg.exe -getactivescheme
# Create a custom object
$PowerSchemes = Get-CimInstance -Name root\cimv2\power -Class Win32_PowerPlan
[System.Collections.ArrayList]$PowerPlan = @()
Foreach ( $Scheme in $PowerSchemes ) {
$PowerPlan += [pscustomobject]@{
Name = $Scheme.elementname
ID = $Scheme.InstanceID.Substring(21,36)
# Or
#ID $Scheme.InstanceID.split('\')[-1].trim('{}')
Active = $Scheme.IsActive
}}
$PowerPlan
# Change the power mode
Get-CimInstance -Name root\cimv2\power -Class Win32_PowerPlan |
Where-Object ElementName -eq "Power saver" |
Invoke-CimMethod -MethodName Activate
# or
$GUID = 'a1841308-3541-4fab-bc81-f71556f20b4a'
Powercfg.exe -SETACTIVE $GUID
# 381b4222-f694-41f0-9685-ff5bb260df2e Balanced
# 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c High performance
# a1841308-3541-4fab-bc81-f71556f20b4a Power saver

This can be turned into two simple functions The first to display the power mode

function Get-PowerPlan {
Param (
[switch]
$Active,
[switch]
$List )
# List power modes
$PowerSchemes = Get-CimInstance -Name root\cimv2\power -Class Win32_PowerPlan
# Creation of a custom object
[System.Collections.ArrayList]$PowerPlan = @()
Foreach ( $Scheme in $PowerSchemes ) {
$PowerPlan += [pscustomobject]@{
Name = $Scheme.elementname
ID = $Scheme.InstanceID.Substring(21,36)
Active = $Scheme.IsActive
}}
# List the power mode currently in use
if ( $Active ) {
$PowerPlan.Where({$_.active -eq $true}).name }
# Change the power supply mode
if ( $List ) {
$PowerPlan }
}
# Display the active power mode
Get-PowerPlan -Active
# List the power modes
Get-PowerPlan -List

And the second to change the power mode

function Set-PowerPlan {
Param (
[GUID]
$ID = 'a1841308-3541-4fab-bc81-f71556f20b4a',
[string]
$Name,
[ValidateSet("Balanced", "Performance", "Saver")]
$FriendlyName)
# List the power modes
$PowerSchemes = Get-CimInstance -Name root\cimv2\power -Class Win32_PowerPlan
# Change the power mode using the ID
if ( $Name ) {
$null = $PowerSchemes |
Where-Object ElementName -eq $Name |
Invoke-CimMethod -MethodName Activate }
if ( $ID ) {
$null = $PowerSchemes |
Where-Object InstanceID -match $ID |
Invoke-CimMethod -MethodName Activate }
if ( $FriendlyName ) {
switch ($FriendlyName) {
"Balanced" { $null = $PowerSchemes |
Where-Object InstanceID -match '381b4222-f694-41f0-9685-ff5bb260df2e' |
Invoke-CimMethod -MethodName Activate }
"performance" { $null = $PowerSchemes |
Where-Object InstanceID -match '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c' |
Invoke-CimMethod -MethodName Activate }
"Saver" { $null = $PowerSchemes |
Where-Object InstanceID -match 'a1841308-3541-4fab-bc81-f71556f20b4a' |
Invoke-CimMethod -MethodName Activate }
}
}
}
# Activate a plan via its id
Set-PowerPlan -id a1841308-3541-4fab-bc81-f71556f20b4a
# Activate a plan via its name
Set-PowerPlan -Name "High performance"
# Activate the default power-saving mode
Set-PowerPlan
# Use a friendlier name
Set-PowerPlan -FriendlyName Saver

Related links