- 1 year ago
Creating a Power Mode Script in Windows 10
Comment
1 / 1
To create a Power Mode script in Windows 10, you can use PowerShell to change the power plan settings. Here's an example script that demonstrates how to switch between power modes:
powershell$powerModes = @{
'Balanced' = 0
'High performance' = 1
'Power saver' = 2
}
$desiredPowerMode = 'Balanced'
# Check if the desired power mode is valid
if (-not $powerModes.ContainsKey($desiredPowerMode)) {
Write-Host "Invalid power mode: $desiredPowerMode"
exit
}
# Get the GUID of the desired power scheme
$powerSchemeGuid = (Get-WmiObject -Namespace root\cimv2\power -Class Win32_PowerPlan | Where-Object { $_.ElementName -eq $desiredPowerMode }).InstanceID
# Change the power scheme
$powerScheme = Get-CimInstance -Namespace root\cimv2\power -ClassName Win32_PowerPlan -Filter "InstanceID = '$powerSchemeGuid'"
$powerScheme.SetActive()
In the above script:
- The
$powerModes
hashtable defines the available power modes and their corresponding values. You can modify this hashtable to include additional power modes if needed. - The
$desiredPowerMode
variable specifies the power mode you want to switch to. Modify this variable to set your desired power mode, such as'Balanced'
,'High performance'
, or'Power saver'
. - The script checks if the desired power mode is valid by verifying that it exists in the
$powerModes
hashtable. - It retrieves the GUID of the desired power scheme using the
Win32_PowerPlan
WMI class. - Finally, it sets the desired power scheme as active using the
SetActive()
method of theWin32_PowerPlan
CIM instance.
To use the script, save it with a .ps1
extension (e.g., powermode.ps1
), and then run it using PowerShell. When executed, the script will change the power mode to the specified value.
Note that running PowerShell scripts requires appropriate execution policy settings. You may need to adjust the execution policy or run the script with administrative privileges if you encounter any issues related to script execution.