How to use script to change the Power Plan in Windows (PowerShell)
Introduction
This script sample can change the Power Plan in Windows.
Scenarios
Most organizations have a number of physical servers and you can make an impact by configuring the power plans on those systems. The appropriate power plan settings can reduce power consumption. This script can easily change the multiple computers' power plan.
Script
Step 1: Start the PowerShell Console with administrator. To run the script in the Windows PowerShell Console, type the command< Script Path> at the Windows PowerShell Console.
Step 2: If you want to know how to use this script. You can type the command Get-Help C:\Script\ChangePowerPlan.ps1 -Full to display the entire help file for this function, such as the syntax, parameters, or examples.
Example
Example 1: Type C:\Script\ChangePowerPlan.ps1 –PowerPlan "PowerSaver" in the Windows PowerShell Console.
This command line shows how to change the power plan from Balanced to Power saver.
Here are some code snippets for your references.
If($PowerPlan)
{
Switch($PowerPlan)
{
Balanced
{
Write-Verbose "Change the power plan from $CurrentPowerPlan to Balanced."
$Result = ChangePowerPlan -ElementName "Balanced" -ComputerName $CN |`
Select -ExpandProperty ReturnValue
break
}
HighPerformance
{
Write-Ver "Change the power plan from $CurrentPowerPlan to Hgih performance."
$Result = ChangePowerPlan -ElementName "High Performance" -ComputerName $CN |`
Select -ExpandProperty ReturnValue
break
}
PowerSaver
{
Write-Verbose "Change the power plan from $CurrentPowerPlan to Power Saver."
$Result = ChangePowerPlan -ElementName "Power Saver" -ComputerName $CN |`
Select -ExpandProperty ReturnValue
break
}
Defalut
{
"The string parameter not found, please input the correct parameter value."
}
}
If($Result)
{
Write-Host "Successfully change the power plan in Windows '$ComputerName'."
}
Else
{
Write-Host "Failed to chang the power plan in Windows '$ComputerName'."
}
}
}
If($PowerPlan) { Switch($PowerPlan) { Balanced { Write-Verbose "Change the power plan from $CurrentPowerPlan to Balanced." $Result = ChangePowerPlan -ElementName "Balanced" -ComputerName $CN |` Select -ExpandProperty ReturnValue break } HighPerformance { Write-Ver "Change the power plan from $CurrentPowerPlan to Hgih performance." $Result = ChangePowerPlan -ElementName "High Performance" -ComputerName $CN |` Select -ExpandProperty ReturnValue break } PowerSaver { Write-Verbose "Change the power plan from $CurrentPowerPlan to Power Saver." $Result = ChangePowerPlan -ElementName "Power Saver" -ComputerName $CN |` Select -ExpandProperty ReturnValue break } Defalut { "The string parameter not found, please input the correct parameter value." } } If($Result) { Write-Host "Successfully change the power plan in Windows '$ComputerName'." } Else { Write-Host "Failed to chang the power plan in Windows '$ComputerName'." } } }
Prerequisite
Windows PowerShell 2.0
Windows 7 or higher version
Microsoft All-In-One Script Framework is an automation script sample library for IT Professionals. The key value that All-In-One Script Framework is trying to deliver is Scenario-Focused Script Samples driven by IT Pros' real-world pains and needs. The team is monitoring all TechNet forums, IT Pros' support calls to Microsoft, and script requests submitted to TechNet Script Repository. We collect frequently asked IT scenarios, and create script samples to automate the tasks and save some time for IT Pros. The team of All-In-One Script Framework sincerely hope that these customer-driven automation script samples can help our IT community in this script-centric move.