I work from a laptop, both at work and at home. In both cases the laptop is either plugged in or I am running on battery power and in both cases I want to make sure that I am using an appropriate Windows power profile. When the laptop is plugged in I want High performance and when it is running on battery I want Power saver . However, Windows thinks it knows best and only offers two profile via the Power options System Tray icon. The options are whatever is current (ie the one i want to swap away from) and Balanced. I never* want to use Balanced – see above. This requires that I click the ‘more power options’ link and then making selections on the following screens.
Time to see if PowerShell can make this a bit easier…
There may be a way to access this information via other means but the first one that I found was the powercfg.exe utility which is readily accessible from PowerShell and if we execute powercfg /? in the command window or in PowerShell we can see all the options available. First of all lets we execute the utility with the -list parameter so we can see the profiles available on the local computer and also the one that is active.
Let’s make a change…
Executing powercfg with the -setactive parameter followed by the GUID of the profile we want to put into effect shows up the next time we execute powercfg -list.
Now it isn’t too difficult to iterate through the list and allow the user the option to select from the available plans, or in my case to write the function so that it will simply switch from High performance to Power save and back with each execution when the function is called with the appropriate parameter. I’ll be adding this to the code that I execute before I start a presentation so that my laptop is running in High performance for all my demos. Here is the function ‘as is’, make sure to understand what it is doing before you run it on a PC that is important to you. I am not responsible for any unwanted affects that this might cause.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
function Change-PowerPlan{ <# .DESCRIPTION Changes the active power profile. Written by Jonathan Allen - Sept 2014. Available for use in any circumstance so long as this credit is included without change. Wholly untested on any computers other than my laptop - no warranty or guarantee included or implied. .EXAMPLE cls Change-PowerPlan This will let the user select between the available plans .EXAMPLE cls Change-PowerPlan -Swap This will swap the active profile between 'Power saver' and 'High performance' without offering any options .INPUTS Swap parameter #> [CmdletBinding()] Param( [Parameter(Mandatory=$false, ValueFromPipeline=$true, Position=0)] [switch]$Swap = $true ) begin{ $PwrProfiles = $null $PwrProfiles = powercfg -list $c=0 $dt = New-Object system.data.datatable "Profiles" $col1 = New-Object system.data.datatable "ID", ([int]) $col2 = New-Object system.data.datatable "ProfileName",([string]) $col3 = New-Object system.data.datatable "ProfileGUID",([string]) $dt.Columns.add($col1) $dt.Columns.add($col2) $dt.Columns.add($col3) $Guid = $null $Name = $null } process{ foreach($r in $PwrProfiles){ # Get GUID $GuidStart = $r.IndexOf(": ") if($GuidStart -gt 0){ $Guid = $r.Substring($GuidStart+2) $Guid = $r.Substring($GuidStart+2,$Guid.indexof(" ")) } # Get Plan name $NameStart = $r.IndexOf(" (") if($NameStart -gt 0){ $Name = $r.Substring($NameStart+2) $Name = $r.Substring($NameStart+2,$Name.indexof(")")) if($r -like '*) `*'){ $CurrentProfile = $Name } } # Load into table if($Guid -ne $null){ $c++ $row = $dt.newrow() $row.ID = $c $row.ProfileName = $Name $row.ProfileGUID = $GUID $dt.Rows.add($row) } } "Power profiles on this computer:`r`n" | write-output $dt | ft -AutoSize # Display current plan "Current profile is : `"$CurrentProfile`"" | Write-Output if(!($Swap)){ $New = read-host "`r`nSelect the plan you want to use by entering its ID now." $NewGuid = $dt.rows[$new-1].ProfileGUID } else{ if($CurrentProfile -eq 'Power saver'){ $NewName = 'High Performance' } else{ $NewName = 'Power Saver' } $newname | Write-Verbose foreach($row in $dt.rows){ $row | Write-Verbose if($row.Profilename -eq $Newname){ $NewGuid = $row.ProfileGUID " The new guid is $NewGUID" | Write-Verbose } } } try{ powercfg -setactive $NewGuid "Power profile changed to `"$NewName`"." | write-output } catch{ "Failed to change power profile." | write-output } } } |
I am still learning PowerShell so if you can suggest ways that I can improve this code (I think probably the datatable is a little overkill for a start) then please put your versions and amendments in the comments.
* – OK, one day I might but I never have yet
Load comments