Change Power Plan when application starts
Most of the time my computer runs using the Balanced power plan. This turns the display off after 10 minutes and puts the computer to sleep after 30 minutes. So you can imagine how upset I get when the computer goes to sleep after 30 minutes, before it can complete the 2 hour download or 3 hour video transcode.
In the control panel (Control Panel -> Hardware and Sound -> Power Options) you can select different power plans. You can then set up desktop shortcuts to configure the system to use the different power profiles. Sadly this is something that you have to remember to do before you start the special program and again after the special program has completed.
I looked all over the internet for a program that will alter the power options (or power plan) automatically depending on which applications were running. Needless to say I couldn’t find one! So I’ve had to write it myself using PowerShell.
Version 1 of the program is really simple. The program has 2 power states, one for when a special program is running and one for when no special programs are running.
There are a few things you need to do to get yourself going.
- Open a Control Panel -> Hardware and Sound -> Power Options find or create 2 power plans. I changed Balanced to turn off the display after 10 minutes and send the computer to sleep after 30 minutes. I created another power plan called No Sleep which switches the display off after 5 minutes and never puts the computer to sleep.
- Open a command prompt and type:
powercfg list
C:\Users\mrn>powercfg list Existing Power Schemes (* Active) ———————————– Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (Balanced) * Power Scheme GUID: 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c (High performance) Power Scheme GUID: a1841308-3541-4fab-bc81-f71556f20b4a (Power saver) Power Scheme GUID: b1b44a00-a3a0-4cf7-9ef1-a2a1c117dc5f (No Sleep)
- Pick the 2 power schemes from the list and update the variables
$programs_running_cfg
,$programs_running_cfg_guid
,$programs_not_running_cfg
and$programs_not_running_cfg_guid
inside the program. - Next we have to find out the names of the special programs, so we can watch for them. Open a Powershell command line. Type:
Get-Process
. Load in the special program and typeGet-Process
again. Compare the outputs of the command to yield the process name.PS C:\Users\mrn> Get-Process Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName ——- —— —– —– —– —— – ———– 152 15 3364 8776 57 1864 Azureus …
- Do this for each special program you want to add.
- Take the ProcessNames and add them as a quoted comma separated list to the variable
$special_programs
$special_programs = @('Azureus', 'VirtualBox')
# dos> powercfg list
$programs_running_cfg = 'No Sleep'
$programs_running_cfg_guid = 'b1b44a00-a3a0-4cf7-9ef1-a2a1c117dc5f'
$programs_not_running_cfg = 'Balanced'
$programs_not_running_cfg_guid = '381b4222-f694-41f0-9685-ff5bb260df2e'
$loop_delay = 60
while ($True)
{
$special_programs_running = $False
for ($i = 0 ; $i -le $special_programs.Length – 1 ; $i++)
{
Get-Process -ErrorAction SilentlyContinue -Name $special_programs[$i]
$running = $?
if ($running -eq $True)
{
$special_programs_running = $True
# break
}
}
$current = powercfg -getactivescheme
if ($special_programs_running -eq $True)
{
write-host "Special programs running"
if ($current -match $programs_running_cfg_guid -eq $False)
{
write-host "Switching to " $programs_running_cfg
powercfg -setactive $programs_running_cfg_guid
}
else
{
write-host "Power profile already set to " $programs_running_cfg
}
}
else
{
write-host "No special programs running"
if ($current -match $programs_not_running_cfg_guid -eq $False)
{
write-host "Switching to " $programs_not_running_cfg
powercfg -setactive $programs_not_running_cfg_guid
}
else
{
write-host "Power profile already set to " $programs_not_running_cfg
}
}
write-host "Sleeping..."
sleep $loop_delay
}
2 comments
Comment from: Kusaywa [Visitor]
Followed the directions with no problem until I got to “Finally, add the program to your start menu Startup folder.” What am I adding to the Startup folder? I created a Microsoft.PowerShell_profile.ps1 file but when I place that in the Startup folder it only opens up in notepad. Any help would be appreciated as this is exactly what I’m looking for. Thanks
Comment from: davidnewcomb [Member]
It sounds like files with the extension
.ps1
are associated with Notepad and not Powershell. Right click the file in Explorer click Properties, then Change… from the General tab to change the file association Powershell.
Form is loading...