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.!"
}