Automatically Switch Windows Theme When on Laptop Battery

Print View Mobile View

By tweaking Windows power plan settings, you can get the maximum performance out of your laptop battery. Unfortunately, there’s no setting in the power plan that will automatically switch themes on unplugging from the power source. Since Windows Aero skin is one of the biggest power hogger, this is like a must have setting in there, especially while you are travelling and there’s no way to re-charge anytime soon. In such situations what you want is more running time than the pretty look. Don’t fret though, you can get this functionality, in a geeky way.

By running a PowerShell script, you can constantly monitor your laptop battery state and switch themes when it changes. This is the script:

$sav = (Get-WmiObject -Class Win32_Battery -ea 0).BatteryStatus
while ($true)
{
	start-sleep -s 5
	if (((Get-WmiObject -Class Win32_Battery -ea 0).BatteryStatus -eq (1 -or 3 -or 4 -or 5 -or 10 -or 11)) -and ((Get-WmiObject -Class Win32_Battery -ea 0).BatteryStatus -ne $sav))
	{
		& 'C:\Windows\Resources\Ease of Access Themes\Classic.theme'
		$sav = (Get-WmiObject -Class Win32_Battery -ea 0).BatteryStatus
		}
	else
	{
		if ((Get-WmiObject -Class Win32_Battery -ea 0).BatteryStatus -ne $sav)
		{
			& 'C:\Windows\Resources\Themes\Aero.theme'
			$sav = (Get-WmiObject -Class Win32_Battery -ea 0).BatteryStatus
		}
	}
}

Copy-paste script to a text editor and it as a .PS1 file.

Above script is set to work with the default Windows 7 Aero and Classic themes. To use different themes, you just have to edit these two paths with the theme of your choice:

  • Unplug theme: C:\Windows\Resources\Ease of Access Themes\Classic.theme
  • Plugged in theme: C:\Windows\Resources\Themes\Aero.theme

Now run the script by right clicking on it and select Run with PowerShell. A blank command window should open up.

If it is the first time you are running a PowerShell script on your computer, you might get an error with a message like this:

The execution of scripts is disabled on this system. Please see “Get-Help about_signing” for more details.

It’s the “execution policy” security setting built into Windows PowerShell that’s blocking the script. The default execution policy is set to Restricted that means that scripts – including those you write yourself – won’t run.

To view your current execution policy setting, open up PowerShell command prompt, type in below command, and then press ENTER:

Get-ExecutionPolicy

To resolve this issue and run the script, you need to change the setting from Restricted to RemoteSigned. To do this, open PowerShell as administrator and type in:

Set-ExecutionPolicy RemoteSigned

Windows PowerShell

Now you can run self-made scripts and the one’s that are downloaded from the internet.

Try running the .PS1 script again. You will see a blank command window with a blinking cursor. It’s actively monitoring your laptop’s power state.

To run the script silently, that is without the command window popup, create a new VBScript with the following code:

Dim shell,command
command = "powershell.exe -nologo -command C:\path-to\script.ps1"
Set shell = CreateObject("WScript.Shell")
shell.Run command,0

Enter full path to your .PS1 file in line 2, and save this script as a .VBS file. This VBS frame will run the PowerShell script silently in the background without displaying a command window.

That’s all!

You can run the VBScript manually when needed, or add it to your Startup folder to automatically launch on Windows logon. Alternatively, you can set the execution as a scheduled task in the Task Scheduler as well.