0

I have a Powershell script I'm working on to automate some processes of our deployment method for a Windows 10 migration. Part of this script activates the High Performance power plan option using powercfg. It works fine on the desktops we use where the three normal plans (Balanced/HP/Power Saver) are already present/created, but the laptops I'm testing it on only have Balanced present in the "Choose power plan" menu.

Usually I'd click through the GUI to "Create power plan" which asks me if I want to base it off an existing plan. Here I'm able to click the High Performance plan template, then just name it as High Performance and use it that way. I'm looking for a way to do this in the Powershell script so I can run this script on both laptops and desktops alike.

Here's what I have for changing the power plan to High Performance on machines where the HP power plan is already created and in the "Choose..." menu (courtesy of SQL Soldier):

Try {
    $HighPerf = powercfg -l | %{if($_.contains("High performance")) {$_.split()[3]}}

    $CurrPlan = $(powercfg -getactivescheme).split()[3]

    if ($CurrPlan -ne $HighPerf) {powercfg -setactive $HighPerf}
        Write-Host "Power plan has been configured to High performance."
} 
Catch {
    Write-Warning -Message "Unable to set power plan to high performance"
}

I have a variable called $Laptop that when -eq to "y" or "Yes" I perform other actions in the script, so I'd like to wrap this code in an if statement based on $Laptop that would create the power plan then activate it if the machine is a laptop, and perform the above code normally if not. I realize this code isn't exactly ironclad, but we only really a select few models so I'm not trying to make it foolproof.

Please let me know if I can clarify anything.

mrk
  • 1
  • Why don't you just export a plan for the laptops to a file (`powercfg /export`) and if there isn't a High Perf plan, then import (`powercfg /import`)? – BenH Dec 13 '18 at 21:52
  • Why not use Group Policy to specify the active power plan? – StephenP Dec 13 '18 at 23:13
  • @BenH initially thought for some reason that wasn't gonna work, as I was gonna give this to my fellow techs as a script, but I can just put the exported scheme in a folder with this script and have it import that scheme if it's not present. so you're right, that'll work! just trying to figure out how to get it to find the scheme to import! it'll be running off a USB stick, here's my very rudimentary try: – mrk Dec 17 '18 at 14:18
  • $HighPerfPresent = powercfg -l | %{if($_.contains("High performance")) {return $True} if ($HighPerfPresent -eq $False){ powercfg /import ($MyInvocation.MyCommand.Definition -Parent) + scheme.pow } – mrk Dec 17 '18 at 14:19
  • @StephenP we do, but the GPO does not take effect until the machine is deployed/renamed for the user and moved into the correct OU, which can take some time, depending. so we usually enable it manually as we're preparing it for deployment until the GPO can take over afterwards. – mrk Dec 17 '18 at 14:20
  • Here's a bit more of an educated try of the import command: powercfg /import (Split-Path $MyInvocation.MyCommand.Path) + "scheme.pow" – mrk Dec 17 '18 at 14:28
  • I think there might be issues with making your target string. Does this work? `powercfg /import "$(Join-Path (Split-Path $MyInvocation.MyCommand.Path) + scheme.pow)"` – BenH Dec 17 '18 at 18:10
  • @BenH no, getting a "positional parameter cannot be found that accepts argument "scheme.pow" error. I was also trying to make a variable called $PowerPlanPath with that line but couldn't get it to work either. – mrk Dec 17 '18 at 18:55
  • @BenH so it seems I've got the variable `$PowerPlanPath` to work and contain the path I need it to contain, checking it with `Write-Host $PowerPlanPath` afterwards, but I keep getting invalid parameter errors for the powercfg command. here's my code so far: `$PowerPlanPath = Join-Path(Split-Path $MyInvocation.MyCommand.Path) "scheme.pow" powercfg /import "$PowerPlanPath" -ErrorAction Stop` – mrk Dec 17 '18 at 19:05
  • @BenH seems that the error comes from the -`ErrorAction` within the cmd, so I guess I'll take it out of the Try/Catch block and rely on the if statement so I can dispose of the `ErrorAction` arguments. did notice that when I commented it out in the Try block the script would execute but close automatically, even though I have a `pause` at the end. getting there... thanks for your help. – mrk Dec 17 '18 at 19:18
  • `-ErrorAction` is a parameter for PowerShell cmdlets. `powercfg.exe` is a standalone application/executable. – BenH Dec 19 '18 at 12:39

0 Answers0