How can I make a PowerShell script to check the current Power plan, change it to "High performance" (if it's not already), then run a long PowerShell script, then after the script, switch back to the original power plan?
I came up with something like this, but it feels like the -and statements fail to work, or am I doing something terribly wrong?
I got the following at this moment:
$PowerSettingsorg = Get-WmiObject -Namespace root\cimv2\power -Class win32_powerplan
$PowerSettings = Get-WmiObject -Namespace root\cimv2\power -Class win32_powerplan
If ($PowerSettings.IsActive -eq $True -And $PowerSettings.ElementName -eq 'Hoge prestaties')
{
write-host "++ Power Plan Settings are correct.!"
}
Else {
$hpPlan = Get-WmiObject -Namespace root\cimv2\power -Class win32_powerplan | Where-Object { $_.ElementName -eq 'Hoge prestaties' }
$hpPlan.Activate()
write-host "++ Power plan Settings are changed to High Performance.!"
}
<Long Script here>
If ($PowerSettingsorg.IsActive -eq 'true' -and $PowerSettingsorg.ElementName -eq 'Gebalanceerd')
{
$orgPlan = Get-WmiObject -Namespace root\cimv2\power -Class win32_powerplan | Where-Object { $_.ElementName -eq 'Gebalanceerd' }
$orgPlan.Activate()
write-host "++ Power plan Settings have been reverted to Belanced.!"
}
Else {
write-host "++ No Power Plan settings have been reverted.!"
}
In Windows 10, the Activate()
method on the COM object does not work anymore, giving you error message Exception calling "Activate" : "This method is not implemented in any class "
.
To manipulate the power scheme settings you will have to make use of PowerCfg.exe
:
# fill a hashtable with power scheme guids and alias names:
# Name Value
# ----- -----
# 381b4222-f694-41f0-9685-ff5bb260df2e SCHEME_BALANCED # --> Balanced
# 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c SCHEME_MIN # --> High performance
# a1841308-3541-4fab-bc81-f71556f20b4a SCHEME_MAX # --> Power saver
$powerConstants = @{}
PowerCfg.exe -ALIASES | Where-Object { $_ -match 'SCHEME_' } | ForEach-Object {
$guid,$alias = ($_ -split '\s+', 2).Trim()
$powerConstants[$guid] = $alias
}
# get a list of power schemes
$powerSchemes = PowerCfg.exe -LIST | Where-Object { $_ -match '^Power Scheme' } | ForEach-Object {
$guid = $_ -replace '.*GUID:\s*([-a-f0-9]+).*', '$1'
[PsCustomObject]@{
Name = $_.Trim("* ") -replace '.*\(([^)]+)\)$', '$1' # LOCALIZED !
Alias = $powerConstants[$guid]
Guid = $guid
IsActive = $_ -match '\*$'
}
}
# set a variable for the desired power scheme (in this case High performance)
$desiredScheme = $powerSchemes | Where-Object { $_.Alias -eq 'SCHEME_MIN' }
# get the currently active scheme
$PowerSettingsorg = $powerSchemes | Where-Object { $_.IsActive }
if ($PowerSettingsorg.Alias -eq $desiredScheme.Alias) {
# or by guid: if ($PowerSettingsorg.Guid -eq $desiredScheme.Guid)
# or localized: if ($PowerSettingsorg.Name -eq $desiredScheme.Name)
# or: if ($desiredScheme.IsActive)
Write-Host "++ Power Plan Settings are correct.! ($($PowerSettingsorg.Name))"
}
else {
# set powersettings to High Performance
Powercfg.exe -SETACTIVE $desiredScheme.Alias # you can also set this using the $desiredScheme.Guid
# test if the setting has changed
$currentPowerGuid = (Powercfg.exe -GETACTIVESCHEME) -replace '.*GUID:\s*([-a-f0-9]+).*', '$1'
if ($currentPowerGuid -eq $desiredScheme.Guid) {
Write-Host "++ Power plan Settings have changed to $($desiredScheme.Name).!"
}
else {
# exit the script here???
Throw "++ Power plan Settings did not change to $($desiredScheme.Name).!"
}
}
### <Long Script here> ###
# get the current power scheme and test if it needs to be reverted
if ($currentPowerGuid -ne $PowerSettingsorg.Guid) {
# set powersettings to the original scheme
Powercfg.exe -SETACTIVE $PowerSettingsorg.Alias # you can also set this using the $PowerSettingsorg.Guid
# test if the setting has changed
$currentPowerGuid = (Powercfg.exe -GETACTIVESCHEME) -replace '.*GUID:\s*([-a-f0-9]+).*', '$1'
if ($currentPowerGuid -eq $PowerSettingsorg.Guid) {
Write-Host "++ Power plan Settings have been reverted to $($PowerSettingsorg.Name).!"
}
else {
# exit the script here???
Throw "++ Power plan Settings did not revert to $($PowerSettingsorg.Name).!"
}
}
else {
Write-Host "++ Power Plan settings did not need to be reverted.!"
}