Add and Activate Ultimate Performance Profile

functions/public/Invoke-WPFUltimatePerformance.ps1
 1Function Invoke-WPFUltimatePerformance {
 2    <#
 3
 4    .SYNOPSIS
 5        Enables or disables the Ultimate Performance power scheme based on its GUID.
 6
 7    .PARAMETER State
 8        Specifies whether to "Enable" or "Disable" the Ultimate Performance power scheme.
 9
10    #>
11    param(
12        [Parameter(Mandatory = $true)]
13        [ValidateSet("Enable", "Disable")]
14        [string]$State
15    )
16
17    try {
18        # GUID of the Ultimate Performance power plan
19        $ultimateGUID = "e9a42b02-d5df-448d-aa00-03f14749eb61"
20
21        switch ($State) {
22            "Enable" {
23                # Duplicate the Ultimate Performance power plan using its GUID
24                $duplicateOutput = powercfg /duplicatescheme $ultimateGUID
25
26                $guid = $null
27                $nameFromFile = "ChrisTitus - Ultimate Power Plan"
28                $description = "Ultimate Power Plan, added via WinUtils"
29
30                # Extract the new GUID from the duplicateOutput
31                foreach ($line in $duplicateOutput) {
32                    if ($line -match "\b[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\b") {
33                        $guid = $matches[0]  # $matches[0] will contain the first match, which is the GUID
34                        Write-Output "GUID: $guid has been extracted and stored in the variable."
35                        break
36                    }
37                }
38
39                if (-not $guid) {
40                    Write-Output "No GUID found in the duplicateOutput. Check the output format."
41                    exit 1
42                }
43
44                # Change the name of the power plan and set its description
45                $changeNameOutput = powercfg /changename $guid "$nameFromFile" "$description"
46                Write-Output "The power plan name and description have been changed. Output:"
47                Write-Output $changeNameOutput
48
49                # Set the duplicated Ultimate Performance plan as active
50                $setActiveOutput = powercfg /setactive $guid
51                Write-Output "The power plan has been set as active. Output:"
52                Write-Output $setActiveOutput
53
54                Write-Host "> Ultimate Performance plan installed and set as active."
55            }
56            "Disable" {
57                # Check if the Ultimate Performance plan is installed by GUID
58                $installedPlan = powercfg -list | Select-String -Pattern "ChrisTitus - Ultimate Power Plan"
59
60                if ($installedPlan) {
61                    # Extract the GUID of the installed Ultimate Performance plan
62                    $ultimatePlanGUID = $installedPlan.Line.Split()[3]
63
64                    # Set a different power plan as active before deleting the Ultimate Performance plan
65                    $balancedPlanGUID = "381b4222-f694-41f0-9685-ff5bb260df2e"
66                    powercfg -setactive $balancedPlanGUID
67
68                    # Delete the Ultimate Performance plan by GUID
69                    powercfg -delete $ultimatePlanGUID
70
71                    Write-Host "Ultimate Performance plan has been uninstalled."
72                    Write-Host "> Balanced plan is now active."
73                } else {
74                    Write-Host "Ultimate Performance plan is not installed."
75                }
76            }
77            default {
78                Write-Host "Invalid state. Please use 'Enable' or 'Disable'."
79            }
80        }
81    } catch {
82        Write-Error "Error occurred: $_"
83    }
84}