The following is a PowerShell script that I wrote to change the Power Plans on my laptop. The script checks which plan I’m currently using, either “Power saver” or “High performance”, and switches to using the other one. I find this functionality helpful, as I can simply create a single shortcut on the desktop (or wherever) that can be executed to instantly change the power plan, without bothering to go through menus. Plus, it’s an excuse to play around with PowerShell.

Code after the jump.


function GetGuidsOfPowerSchemes
{
	param(
		[ref]$highPerformanceSchemeGuid,
		[ref]$powerSaverSchemeGuid,
		$powercfgOutput)

	$powerSchemeGuidString = "Power Scheme GUID: "
	$highPerformanceString = "(High Performance)"
	$powerSaverString = "(Power saver)"
	$highPerformanceSchemeGuid.Value = $null
	$powerSaverSchemeGuid.Value = $null

	foreach($config in $powercfgOutput)
	{
		$configsString = New-Object
			-TypeName "System.String"
			-ArgumentList "$config"
		if (
			$configsString.IndexOf(
				$powerSchemeGuidString,
				[System.StringComparison]::OrdinalIgnoreCase)
			-eq 0)
		{
			$guid = New-Object
				-TypeName "System.Guid"
				-ArgumentList (
					$configsString.Substring(
						$powerSchemeGuidString.Length,
						36))
			if (
				$configsString.IndexOf(
					$highPerformanceString,
					[System.StringComparison]::OrdinalIgnoreCase)
				-gt 0)
			{
				$highPerformanceSchemeGuid.Value = $guid
			}
			elseif (
				$configsString.IndexOf(
					$powerSaverString,
					[System.StringComparison]::OrdinalIgnoreCase)
				-gt 0)
			{
				$powerSaverSchemeGuid.Value = $guid
			}
		}
	}
}

$garbage = [Reflection.Assembly]::LoadWithPartialName("System.Text")

$highPerformanceSchemeGuid = $null
$powerSaverSchemeGuid = $null
$configsList = powercfg -list
GetGuidsOfPowerSchemes
	-powercfgOutput $configsList
	-highPerformanceSchemeGuid ([ref]$highPerformanceSchemeGuid)
	-powerSaverSchemeGuid ([ref]$powerSaverSchemeGuid)

$highPerformanceSchemeActiveGuid = $null
$powerSaverSchemeActiveGuid = $null
$configsList = powercfg -getactivescheme
GetGuidsOfPowerSchemes
	-powercfgOutput $configsList
	-highPerformanceSchemeGuid ([ref]$highPerformanceSchemeActiveGuid)
	-powerSaverSchemeGuid ([ref]$powerSaverSchemeActiveGuid)

Write-Host "Active schemes"
if ($highPerformanceSchemeActiveGuid -ne $null 
	-and $powerSaverSchemeGuid -ne $null)
{
	Write-Host "High power, setting to low"
	$garbage = powercfg -setactive $powerSaverSchemeGuid
}
elseif ($powerSaverSchemeActiveGuid -ne $null
	-and $highPerformanceSchemeGuid -ne $null)
{
	Write-Host "Low power, setting to high"
	$garbage = powercfg -setactive $highPerformanceSchemeGuid
}
elseif ($highPerformanceSchemeGuid -ne $null)
{
	Write-Host "Power setting not determined, setting to high"
	$garbage = powercfg -setactive $highPerformanceSchemeGuid
}
else
{
	Write-Host "Unable to determine current setting and cannot find high setting"
}

All the real work is done by an application called powercfg. The script just gets the available power schemes, checks which scheme is now active and sets the other scheme (identified by GUIDs) to be the active one. Pretty straightforward stuff, but still required me to “parse” the input. Of course, a regex would have worked better, but that’s yet another thing I need to learn.

Advertisements