Created
July 23, 2025 23:07
-
-
Save ElianFabian/6297dd3724e7beea428f01a8e2d3c7a2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# PowerShell script to optimize the CPU usage to avoid doing it manually in the Windows Panel Control. | |
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltinRole] "Administrator")) { | |
Write-Host "No eres administrador, reiniciando script con permisos elevados..." -ForegroundColor Yellow | |
$newProcess = Start-Process powershell "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs -PassThru | |
$newProcess.WaitForExit() | |
exit | |
} | |
function ExecuteWithEnglishLanguage { | |
param ( | |
[ScriptBlock] $ScriptBlock | |
) | |
$previousLanguage = Get-SystemLanguage | |
Set-SystemLanguage 'en-US' | |
try { | |
& $ScriptBlock | |
} | |
finally { | |
Set-SystemLanguage $previousLanguage | |
} | |
} | |
function Get-ExistingPowerSchemes { | |
ExecuteWithEnglishLanguage { | |
return powercfg /list | Select-Object -Skip 1 ` | |
| Select-String -Pattern 'Power Scheme GUID: (.+) \((.+)\)\s?(\*?)' ` | |
| ForEach-Object { | |
[PSCustomObject]@{ | |
Guid = $_.Matches.Groups[1].Value | |
Name = $_.Matches.Groups[2].Value | |
IsActive = $_.Matches.Groups[3] -and $_.Matches.Groups[3].Value -eq '*' | |
} | |
} | |
} | |
} | |
function Get-MaximumProcessorStateSetting { | |
param ( | |
[Parameter(Mandatory = $true)] | |
[string] $PowerSchemeGuid | |
) | |
ExecuteWithEnglishLanguage { | |
$guidRegex = '^[{(]?[0-9a-fA-F]{8}(-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}[)}]?$' | |
if ($PowerSchemeGuid -notmatch $guidRegex) { | |
throw "Invalid Power Scheme GUID: $PowerSchemeGuid" | |
} | |
$rawValue = powercfg /query $PowerSchemeGuid SUB_PROCESSOR PROCTHROTTLEMAX ` | |
| Select-String -Pattern 'Current AC Power Setting Index: (.+)' ` | |
| ForEach-Object { $_.Matches.Groups[1].Value.Trim() } | |
return [Convert]::ToInt32($rawValue , 16) | |
} | |
} | |
function Set-MaximumProcessorStateSetting { | |
param ( | |
[Parameter(Mandatory = $true)] | |
[string] $PowerSchemeGuid, | |
[Parameter(Mandatory = $true)] | |
[int] $Value | |
) | |
ExecuteWithEnglishLanguage { | |
if ($Value -lt 0 -or $Value -gt 100) { | |
throw "El valor debe estar entre 0 y 100" | |
} | |
$guidRegex = '^[{(]?[0-9a-fA-F]{8}(-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}[)}]?$' | |
if ($PowerSchemeGuid -notmatch $guidRegex) { | |
throw "Invalid Power Scheme GUID: $PowerSchemeGuid" | |
} | |
powercfg /setacvalueindex $PowerSchemeGuid SUB_PROCESSOR PROCTHROTTLEMAX $Value | |
} | |
} | |
function Modify-ProcessorMaxStateInteractive { | |
param ( | |
[array] $Schemes | |
) | |
# Preguntar valor personalizado con validación persistente | |
$customValue = $null | |
$customMode = $false | |
while ($true) { | |
$inputValue = Read-Host "Introduce un valor personalizado entre 0 y 100 (o pulsa Enter para intercambiar entre 99% y 100%)" | |
if ([string]::IsNullOrWhiteSpace($inputValue)) { | |
break | |
} | |
if ($inputValue -match '^\d+$') { | |
$tempValue = [int]$inputValue | |
if ($tempValue -ge 0 -and $tempValue -le 100) { | |
$customValue = $tempValue | |
$customMode = $true | |
break | |
} | |
} | |
Write-Host "Valor invalido. Debe ser un numero entre 0 y 100 o simplemente pulsa Enter." -ForegroundColor Red | |
} | |
# Determinar cambios | |
$changes = @() | |
foreach ($scheme in $Schemes) { | |
try { | |
$currentValue = Get-MaximumProcessorStateSetting -PowerSchemeGuid $scheme.Guid | |
$newValue = $null | |
if ($customMode) { | |
$newValue = $customValue | |
} | |
elseif ($currentValue -lt 100) { | |
$newValue = 100 | |
} | |
elseif ($currentValue -eq 100) { | |
$newValue = 99 | |
} | |
else { | |
Write-Host "[$($scheme.Name)] Valor actual $currentValue%, fuera de la logica. No se modifica." -ForegroundColor Yellow | |
continue | |
} | |
$changes += [PSCustomObject]@{ | |
Scheme = $scheme | |
NewValue = $newValue | |
CurrentValue = $currentValue | |
} | |
} | |
catch { | |
Write-Host "Error al procesar el plan '$($scheme.Name)': $_" -ForegroundColor Red | |
} | |
} | |
if ($changes.Count -eq 0) { | |
Write-Host "No hay cambios pendientes. Saliendo." -ForegroundColor Yellow | |
return | |
} | |
# Mostrar resumen | |
Write-Host "`nCambios propuestos:" -ForegroundColor Cyan | |
foreach ($change in $changes) { | |
Write-Host "[$($change.Scheme.Name)] $($change.CurrentValue)% -> $($change.NewValue)%" | |
} | |
Write-Host | |
Write-Host "A que planes quieres aplicar el cambio?" | |
Write-Host "1. Solo al plan activo" | |
Write-Host "2. A todos los planes" | |
Write-Host "3. Cancelar" | |
$choice = Read-Host "Elige una opcion (1-3)" | |
Write-Host | |
switch ($choice) { | |
'1' { | |
$active = $changes | Where-Object { $_.Scheme.IsActive } | |
if ($active) { | |
foreach ($item in $active) { | |
Set-MaximumProcessorStateSetting -PowerSchemeGuid $item.Scheme.Guid -Value $item.NewValue | |
Write-Host "[$($item.Scheme.Name)] actualizado a $($item.NewValue)% (activo)" -ForegroundColor Green | |
} | |
} | |
else { | |
Write-Host "No se encontro plan activo. Nada que hacer." -ForegroundColor Yellow | |
} | |
} | |
'2' { | |
foreach ($item in $changes) { | |
Set-MaximumProcessorStateSetting -PowerSchemeGuid $item.Scheme.Guid -Value $item.NewValue | |
Write-Host "[$($item.Scheme.Name)] actualizado a $($item.NewValue)%" -ForegroundColor Green | |
} | |
} | |
default { | |
Write-Host "Operacion cancelada por el usuario." -ForegroundColor DarkGray | |
} | |
} | |
# Con esto hacemos efectivos los cambios | |
$current = Get-ExistingPowerSchemes | Where-Object { $_.IsActive } | Select-Object -First 1 -ExpandProperty Guid | |
powercfg /setactive SCHEME_BALANCED | |
Start-Sleep -Milliseconds 500 | |
powercfg /setactive $current | |
} | |
try { | |
$OutputEncoding = [Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false) | |
$schemes = Get-ExistingPowerSchemes | |
if ($schemes.Count -eq 0) { | |
Write-Host "No se pudo encontrar ningun plan de energia" -ForegroundColor Red | |
return | |
} | |
foreach ($scheme in $schemes) { | |
$color = if ($scheme.IsActive) { 'Green' } else { 'White' } | |
Write-Host "Plan de energia: $($scheme.Name)" -ForegroundColor Cyan -NoNewline | |
if ($scheme.IsActive) { | |
Write-Host " (Activo)" -ForegroundColor Green | |
} | |
else { | |
Write-Host | |
} | |
try { | |
$maxState = Get-MaximumProcessorStateSetting -PowerSchemeGuid $scheme.Guid | |
Write-Host " Estado maximo del procesador: $maxState%" -ForegroundColor $color | |
} | |
catch { | |
Write-Host " Error al obtener el estado maximo del procesador: $_" -ForegroundColor Red | |
} | |
} | |
Write-Host | |
Modify-ProcessorMaxStateInteractive $schemes | |
} | |
catch { | |
Write-Host "Error: $_" -ForegroundColor Red | |
} | |
Write-Host | |
Write-Host | |
Write-Host | |
Read-Host "Presiona Enter para continuar..." > $null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment