Changing the Power Saving Settings using Windows PowerShell

Our Power Saving settings are set via a policy and quite frankly, I think they are too aggressive…so I decided to set mine using PowerShell.

# Changing the Power Saving Settings via PowerShell
$MyPlan= “Power Saver” # You could use ‘Balanced’, ‘High Performance’, ‘Power Saver’, etc here
Write-Host “Setting Power Plan to $MyPlan”
$guid = (Get-WmiObject -Class win32_powerplan -Namespace rootcimv2power -Filter “ElementName=’$MyPlan'”).InstanceID.tostring()
$regex = [regex]”{(.*?)}$”
$newPower = $regex.Match($guid).groups[1].value
powercfg -S $newPower
Write-Host “Setting Standby Timeout to Never”
POWERCFG -change -standby-timeout-ac 0
Write-Host “Setting Monitor Timeout to Never”
POWERCFG -change -monitor-timeout-ac 0

 

I saved this into a .ps1 file called “PowerSettings.ps1”, wrapped this up into a shortcut and placed it into my startup folder. The shortcut simply runs this command:

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe C:\Scripts\PowerSettings.ps1

Now every time my laptop boots up, it runs the script and changes the settings to my taste.

 
Comments

Thanks for sharing the code. That regex method was what I needed to finish my script.


## check if power scheme exists
$wotsit = Get-CimInstance -N root\cimv2\power -ClassName Win32_PowerPlan
if ($wotsit.Elementname -notcontains 'Always On') {
## generate GUID for new power scheme
$fluffy = [guid]::NewGuid()
## duplicate and modify the power scheme
$commands = {
powercfg -duplicatescheme SCHEME_BALANCED $fluffy
powercfg -changename $fluffy 'Always On' 'No timeouts.'
powercfg -s $fluffy
powercfg -x -monitor-timeout-ac 0
powercfg -x -standby-timeout-ac 0
powercfg -x -hibernate-timeout-ac 0
powercfg -s $fluffy
}
& $commands
} else {
## check if power scheme is active, set active
$right = $wotsit | where {$_.ElementName -eq 'Always On'}
if (!$right.IsActive) {
$regex = [regex]"{(.*?)}$"
powercfg -s $regex.Match($right.InstanceID).groups[1].value
}
}

Glad to have helped 🙂

Leave a Reply