How to use script to change the Power Plan in Windows (VBScript)
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: You can run this VBScript sample by simply double-clicking.
The step is shown in the following figure.
When the script finishes running, it will show a brief message.
Here are some code snippets for your references.
If IsEmpty(value) Then
WScript.Quit
Else
Select Case value
Case "1" objWshShell.Run "powercfg -s 381b4222-f694-41f0-9685-ff5bb260df2e"
CheckCurrentPowerPlan
Case "2" objWshShell.Run "powercfg -s 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c"
CheckCurrentPowerPlan
Case "3" objWshShell.Run "powercfg -s a1841308-3541-4fab-bc81-f71556f20b4a"
CheckCurrentPowerPlan
Case Else MsgBox "You input the wrong option, please enter again"
End Select
Wscript.Sleep 2000
End If
Function CheckCurrentPowerPlan
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer &"\root\cimv2\power")
Set colItems = objWMIService.ExecQuery("Select * From Win32_PowerPlan where isActive='true'")
For Each colItem in colItems
Wscript.Echo "Now, the current power plan option is " & colItem.ElementName
Next
End Function
If IsEmpty(value) Then WScript.Quit Else Select Case value Case "1" objWshShell.Run "powercfg -s 381b4222-f694-41f0-9685-ff5bb260df2e" CheckCurrentPowerPlan Case "2" objWshShell.Run "powercfg -s 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c" CheckCurrentPowerPlan Case "3" objWshShell.Run "powercfg -s a1841308-3541-4fab-bc81-f71556f20b4a" CheckCurrentPowerPlan Case Else MsgBox "You input the wrong option, please enter again" End Select Wscript.Sleep 2000 End If Function CheckCurrentPowerPlan strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer &"\root\cimv2\power") Set colItems = objWMIService.ExecQuery("Select * From Win32_PowerPlan where isActive='true'") For Each colItem in colItems Wscript.Echo "Now, the current power plan option is " & colItem.ElementName Next End Function
Windows PowerShell 2.0
Windows 7 or higher versionMicrosoft 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.