How to Get the Current Power Plan Name in Batch Script
When writing automation scripts for laptops or servers, understanding the current power state of the machine is crucial. For example, a heavy rendering script might refuse to run if the machine is currently on a "Power Saver" plan to avoid draining the battery, or a monitoring tool might want to log the active scheme.
In Windows, power management is handled primarily by the powercfg utility. In this guide, we will explore how to use powercfg to extract not just the GUID (the internal system ID) of the active power plan, but also its human-readable name using Batch Script parsing techniques.
The Challenge of powercfg Output
If you type powercfg /getactivescheme into a command prompt, the output looks something like this:
Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (Balanced)
While this is easily readable by a human, a Batch Script cannot use this raw string directly for logic or clean logging. We must parse this string to extract the specific pieces of information we need.
Method 1: Extracting Only the Power Plan Name
To get the human-readable name (e.g., "Balanced", "High performance", or "My Custom Plan"), we need to extract the text inside the parentheses at the end of the powercfg output.
We can achieve this using a for /f loop to split the string based on specific delimiter characters.
@echo off
setlocal
echo Retrieving Active Power Plan Name...
:: Run powercfg and capture its output
for /f "tokens=2 delims=()" %%A in ('powercfg /getactivescheme') do (
set "plan_name=%%A"
)
echo The active power plan is: %plan_name%
pause
How the Parsing Works
'powercfg /getactivescheme'executed inside single quotes tells theforloop to capture the command's output.delims=(): This sets both the open parenthesis(and close parenthesis)as separators.- By splitting the string
Power Scheme GUID: 1234... (Balanced)using parentheses, the first token is everything before the(, and the second token (tokens=2) is the wordBalanced.
This method handles custom power plan names perfectly, even if the name contains spaces (e.g., "(Company Presentation Mode)").
Method 2: Extracting Both the GUID and the Name
Sometimes your script needs both pieces of information: the friendly name for logging or UI display, and the GUID for actually modifying the plan later in the script.
Parsing both with a single for /f command when dealing with both spaces and parentheses can be tricky. The most reliable approach is to use two separate for /f loops, each with its own delimiter strategy.
@echo off
setlocal
:: Get the GUID (Token 4 separated by spaces)
for /f "tokens=4" %%G in ('powercfg /getactivescheme') do (
set "plan_guid=%%G"
)
:: Get the Name (Token 2 separated by parentheses)
for /f "tokens=2 delims=()" %%N in ('powercfg /getactivescheme') do (
set "plan_name=%%N"
)
echo Friendly Name: %plan_name%
echo System GUID: %plan_guid%
:: Example usage: Switch logic based on GUID
if "%plan_guid%"=="8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c" (
echo [INFO] System is optimized for High Performance.
) else (
echo [WARNING] System is not in High Performance mode. Performance may be degraded.
)
pause
Standard Windows Power Plan GUIDs
For reference, the default Windows power plans have hardcoded GUIDs that never change across installations:
- Balanced:
381b4222-f694-41f0-9685-ff5bb260df2e - High performance:
8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c - Power saver:
a1841308-3541-4fab-bc81-f71556f20b4a
Method 3: Handling Multi-Language Environments
If your script will run on non-English versions of Windows (e.g., French or Spanish), you should never rely on the plan_name for script logic.
For example, on a Spanish system, the "Balanced" plan is named "Equilibrado".
:: WRONG - Will fail on non-English machines
if "%plan_name%"=="High performance" goto proceed
Output Concern: Your script will fail to detect the high-performance state on international machines.
The Correct Approach: Always Logic Check the GUID
:: CORRECT - GUIDs are universal
if "%plan_guid%"=="8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c" goto proceed
Use the plan_name exclusively for echo statements so the user sees the plan in their native language, but use plan_guid for all internal if statements.
Method 4: PowerShell Alternative
If you prefer to avoid text parsing entirely, PowerShell can query the active power plan through WMI, though it is slightly slower to execute in Batch due to the PowerShell overhead.
@echo off
setlocal
echo Executing WMI Query for Power Plan...
powershell -command ^
"$plan = Get-WmiObject -Class Win32_PowerPlan -Namespace root\cimv2\power -Filter 'IsActive=True';" ^
"Write-Host 'Active Plan:' $plan.ElementName"
pause
This method directly queries the Win32_PowerPlan class, grabbing the ElementName property without needing to clip strings or count delimiters. It is highly robust.
Best Practices
- Parse carefully:
powercfgoutput is very consistent, makingdelims=()the perfect trick for grabbing friendly names. - Use GUIDs for Logic: Never write
If "%name%"=="Balanced". Always use the known GUIDs for system-default plans to ensure your script is universally compatible. - Display the Name for Users: Users don't know what
381B4...means. Always parse and echo the friendly name to the console when logging actions or prompting for confirmation.
Conclusion
Getting the current power plan name in a Batch Script bridges the gap between machine-readable configurations (GUIDs) and user-friendly console output. By utilizing powercfg /getactivescheme in conjunction with a for /f loop utilizing parentheses as delimiters, you can effortlessly extract the human-readable plan name. Coupled with best practices for international compatibility, this technique is a staple for writing polished system monitoring and automation scripts.