<# .SYNOPSIS Reverts the power-saving/hibernate changes made by Test-BatteryDrain.ps1. .DESCRIPTION 1. Re-launches itself elevated if not running as Administrator (powercfg changes require admin). 2. Resets all built-in power schemes (Balanced, Power saver, High performance) to their Windows factory defaults, which undoes the timeout/USB/PCIe/battery-action tweaks applied by the test script. 3. Removes the "Ultimate Performance" scheme duplicate if the test script created one. 4. Restores hibernation to its original on/off state, and re-activates whichever power scheme was active before the test ran (both read from the state file saved by Test-BatteryDrain.ps1). .PARAMETER StateFilePath Path to the JSON state file written by Test-BatteryDrain.ps1. Defaults to .\PowerSettingsBackup.json next to this script. .EXAMPLE .\Restore-PowerSettings.ps1 #> [CmdletBinding()] param( [string]$StateFilePath = $(Join-Path -Path $PSScriptRoot -ChildPath 'PowerSettingsBackup.json') ) $ErrorActionPreference = 'Stop' #region Elevation check function Test-IsAdmin { $identity = [Security.Principal.WindowsIdentity]::GetCurrent() $principal = New-Object Security.Principal.WindowsPrincipal($identity) return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) } if (-not (Test-IsAdmin)) { Write-Host "Not running elevated - relaunching as Administrator..." -ForegroundColor Yellow Start-Process -FilePath 'powershell.exe' -ArgumentList @( '-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', "`"$PSCommandPath`"", '-StateFilePath', "`"$StateFilePath`"" ) -Verb RunAs exit } #endregion Write-Host "Restoring default power schemes (undoes timeout/USB/PCIe/battery-action changes)..." -ForegroundColor Cyan powercfg -restoredefaultschemes | Out-Null # Remove the Ultimate Performance duplicate scheme created by Test-BatteryDrain.ps1, if present. $ultimateGuid = (powercfg /list | Select-String -Pattern 'Ultimate Performance' | ForEach-Object { if ($_ -match '([0-9a-fA-F-]{36})') { $Matches[1] } }) | Select-Object -First 1 $state = $null if (Test-Path $StateFilePath) { $state = Get-Content -Path $StateFilePath -Raw | ConvertFrom-Json } else { Write-Host "No state file found at $StateFilePath - will restore hibernate to 'on' and leave the active scheme as Balanced." -ForegroundColor Yellow } # Re-activate whichever scheme was active before the test, so we don't strand the machine on the # duplicated Ultimate Performance scheme right before deleting it. if ($state -and $state.OriginalSchemeGuid) { try { powercfg /setactive $state.OriginalSchemeGuid | Out-Null Write-Host "Restored original active power scheme ($($state.OriginalSchemeGuid))." -ForegroundColor Green } catch { Write-Host "Could not restore original scheme GUID; falling back to Balanced." -ForegroundColor Yellow powercfg /setactive SCHEME_BALANCED | Out-Null } } else { powercfg /setactive SCHEME_BALANCED | Out-Null Write-Host "Active plan set to Balanced (default)." -ForegroundColor Green } if ($ultimateGuid) { try { powercfg /delete $ultimateGuid | Out-Null Write-Host "Removed the Ultimate Performance scheme created for the test." -ForegroundColor Green } catch { Write-Host "Could not remove Ultimate Performance scheme (it may still be active); skipping." -ForegroundColor Yellow } } # Restore hibernate to its original state (defaults to "on" if no state file is available). $hibernateEnabled = if ($state) { [bool]$state.HibernateEnabled } else { $true } if ($hibernateEnabled) { powercfg /hibernate on | Out-Null Write-Host "Hibernate re-enabled." -ForegroundColor Green } else { powercfg /hibernate off | Out-Null Write-Host "Hibernate left disabled (it was already off before the test)." -ForegroundColor Green } if (Test-Path $StateFilePath) { Remove-Item -Path $StateFilePath -Force } Write-Host "Power settings restored to their pre-test state." -ForegroundColor Yellow