Edit a PoSh script to Adjust the Power Mode Slider in Windows 10

MAS2487 66 Reputation points
2023-03-29T12:57:29.15+00:00
Hello, everyone. I want to use a PowerShell script to adjust the Power Mode slider in Windows 10 from the default ‘Balanced’ to ‘Best Performance’. I found an existing script that cycles through the 3 power mode options to correct a specific issue. However I would like to massage it to just do a one time change from balanced to best performance. Any help is greatly appreciated!
Here is the existing script:


=========================================================================================
# Toggle power mode away from and then back to effective overlay 
$togglePowerOverlay = {
    $function = @'
    [DllImport("powrprof.dll", EntryPoint="PowerSetActiveOverlayScheme")]
    public static extern int PowerSetActiveOverlayScheme(Guid OverlaySchemeGuid);
    [DllImport("powrprof.dll", EntryPoint="PowerGetActualOverlayScheme")]
    public static extern int PowerGetActualOverlayScheme(out Guid ActualOverlayGuid);
    [DllImport("powrprof.dll", EntryPoint="PowerGetEffectiveOverlayScheme")]
    public static extern int PowerGetEffectiveOverlayScheme(out Guid EffectiveOverlayGuid);
'@
    $power = Add-Type -MemberDefinition $function -Name "Power" -PassThru -Namespace System.Runtime.InteropServices
    
    $modes = @{
        "better_battery"     = [guid] "961cc777-2547-4f9d-8174-7d86181b8a7a";
        "better_performance" = [guid] "00000000000000000000000000000000";
        "best_performance"   = [guid] "ded574b5-45a0-4f42-8737-46345c09c238"
    }
    
    $actualOverlayGuid = [Guid]::NewGuid()
    $ret = $power::PowerGetActualOverlayScheme([ref]$actualOverlayGuid)
    if ($ret -eq 0) {
        "Actual power overlay scheme: $($($modes.GetEnumerator()|where {$_.value -eq  $actualOverlayGuid}).Key)." | Write-Host
    }
    
    $effectiveOverlayGuid = [Guid]::NewGuid()
    $ret = $power::PowerGetEffectiveOverlayScheme([ref]$effectiveOverlayGuid)
    
    if ($ret -eq 0) {        
        "Effective power overlay scheme: $($($modes.GetEnumerator() | where { $_.value -eq  $effectiveOverlayGuid }).Key)." | Write-Host
        
        $toggleOverlayGuid = if ($effectiveOverlayGuid -ne $modes["best_performance"]) { $modes["best_performance"] } else { $modes["better_performance"] }     
        
        # Toggle Power Mode
        $ret = $power::PowerSetActiveOverlayScheme($toggleOverlayGuid)
        if ($ret -eq 0) {
            "Toggled power overlay scheme to: $($($modes.GetEnumerator()| where { $_.value -eq  $toggleOverlayGuid }).Key)." | Write-Host
        }

        $ret = $power::PowerSetActiveOverlayScheme($effectiveOverlayGuid)
        if ($ret -eq 0) {
            "Toggled power overlay scheme back to: $($($modes.GetEnumerator()|where {$_.value -eq  $effectiveOverlayGuid }).Key)." | Write-Host
        }
    }
    else {
        "Failed to toggle active power overlay scheme." | Write-Host      
    }
}

# Execute the above
& $togglePowerOverlay
==========================================================================================

Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
10,756 questions
Windows 10 Hardware Performance
Windows 10 Hardware Performance
Windows 10: A Microsoft operating system that runs on personal computers and tablets.Hardware Performance: Delivering / providing hardware or hardware systems or adjusting / adapting hardware or hardware systems.
96 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,132 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more