Public/OS/Power/Set-PowerMode.ps1

<#
Copyright © 2024 Integris. For internal company use only. All rights reserved.
#>


FUNCTION Set-PowerMode {
    <#
    .SYNOPSIS
    Sets the power mode of the system to the specified profile.
 
    .DESCRIPTION
    This function changes the system's power mode to one of the predefined profiles: "Best Power Efficiency," "Balanced," or "Best Performance." It uses the PowerSetActiveOverlayScheme method from the powrprof.dll to apply the selected power mode.
 
    .PARAMETER Mode
    Specifies the power mode to set. Valid values are "Best Power Efficiency," "Balanced," and "Best Performance."
 
    .EXAMPLE
    Set-PowerMode -Mode "Best Performance"
 
    This command sets the system's power mode to "Best Performance."
 
    .NOTES
    This function requires the powrprof.dll library and may need to be run with appropriate permissions.
    #>


    [CmdletBinding()]
    param (
        [ValidateSet('Best Power Efficiency', 'Balanced', 'Best Performance')]
        [string]$Mode
    )

    $ModeToGuid = @{
        "Best Power Efficiency" = [guid] "961cc777-2547-4f9d-8174-7d86181b8a7a";
        "Balanced" = [guid] "00000000-0000-0000-0000-000000000000";
        "Best Performance" = [guid] "ded574b5-45a0-4f42-8737-46345c09c238"
    }
    
    $guid = $ModeToGuid[$Mode]
    
    $function = @'
    [DllImport("powrprof.dll", EntryPoint="PowerSetActiveOverlayScheme")]
    public static extern int PowerSetActiveOverlayScheme(Guid OverlaySchemeGuid);
'@


    $power = Add-Type -MemberDefinition $function -Name "Power" -PassThru -Namespace MyNamespace.PowerModeManager

    $ret = $power::PowerSetActiveOverlayScheme($guid)

    if ($ret -ne 0) {
        Write-Error "Failed to set the power mode profile ID."
    }
}