Using Batch file to change various settings in Windows Control Panel

13.3k views Asked by At

I am looking for how to make several changes in windows control panel settings on a computer running Windows 7, by using a batch file (without powershell).

The required settings are :

In PowerCFG Panel :

  • Monitor Timeout on AC (switch to Disable)
  • HDD Timeout on AC (switch to Disable)
  • Standby Timeout on AC (switch to Disable)
  • Hibernate Timeout on AC (switch to Disable)
  • Action of the hardware power button (switch to Disable)

In Start Menu Panel :

  • Action of the shutdown button (switch to Change User)

I have already tried this script :

@Echo Off
POWERCFG /CHANGE monitor-timeout-ac 0
POWERCFG /CHANGE disk-timeout-ac 0
POWERCFG /CHANGE standby-timeout-ac 0
POWERCFG /CHANGE hibernate-timeout-ac 0
exit

But I did not find how to get the others settings.

Thank you Forward

1

There are 1 answers

3
J.Baoby On

Before I explain you my solutions, I would advise you to copy the current power scheme and work with that copy instead of changing the current scheme. The current scheme may be a default scheme and changing without backup is sometimes risky. Undoing configuration changes is not always that easy.

I have 2 solutions I would like to share with you. The first one presents a solution for the hardware power button. It may also be a solution for the shutdown in the start menu but I couldn't set it on "Change User". I only tested it on a Windows 10 machine however (I had no Windows 7 machine to test it on) so there is a possibility that it could even be working for the start menu button on a windows 7 (could someone confirm this?). The problem is that it seems like the shutdown button in the start menu from windows 10 is not really the same as in windows 7 (windows 10 has 2 separate buttons in its start menu: one for the the log off and switch user, and one for the shutdown, sleep and restart; windows 7 has one button for all options).
The second solution is a second option for the shutdown in the start menu that surely works for Windows 7 (I used it some time ago).
Both solutions may require a restart, espacially the first one.

  1. There actually are more settings you can change with powercfg then the ones available with the /CHANGE (or -change or -x) flags. The hardware power button is one of them. There is also a setting for the shutdown button in the start menu but I'm not sure if it can help. Anyway, to check all settings you can change for a power scheme you'll need the power scheme's GUID (a 36 character long ID). powercfg always gives you the possibility to use aliases instead of GUID. You can choose but GUIDs have a specific form that makes it easier to use in scripts (except maybe for some cases). The aliases are just easier to read, understand and remember.

    To search for the GUID of a scheme you can use: powercfg -list which will print a string of the form Power Scheme GUID: <GUID> (<NAME POWER SCHEME>) for each power scheme (name is not the same as alias). You can then use findstr to choose a specific power scheme name (or just powercfg -getactivescheme for only the active scheme) and use a FOR /F to extract the GUID like this:

    FOR /F "tokens=4" %%G IN ('powercfg /getactivescheme') DO set activeschemeGUID=%%G
    

    Once you have the GUID (or alias) you can check its whole configuration with:

    powercfg -q <Scheme_GUID>
    

    powercfg -q (or powercfg -q %activeschemeGUID% with the variable from first code example) will give you the GUID, alias and (the long list of) all settings for the active scheme you can change and the values they can take (in powercfg they actually name it value index and not value).
    Settings are grouped in subgroups. Each subgroup and setting has a GUID. The most common subgroups and settings even have aliases. To check all avialable aliases for settings and subgroups you can use powercfg -aliases. You'll need the GUID (or the alias if available) to check which values a setting can take and its current value:

    powercfg -q <Scheme_GUID> <SUB_GUID> <Setting_GUID>
    

    or to change its value(s):

    powercfg -setacvalueindex <Scheme_GUID> <SUB_GUID> <Setting_GUID> <New_value_index> 
    powercfg -setdcvalueindex <Scheme_GUID> <SUB_GUID> <Setting_GUID> <New_value_index>
    

    The first one will change the AC value index and the second one the DC (if available, check the values of the setting therefore).

    The only thing you now need is the correct GUIDs for your settings (or aliases if available). I'll spare you the search time and give you the GUIDs on my side. They should be default but you can always look for them by searching the string "power" (case-insensitive search) in all available settings. The setting for the action of the hardware power button and the setting for the action of the shutdown button in the start menu both belong to the same subgroup: subgroup with alias SUB_BUTTONS and GUID 4f971e89-eebd-4455-a8de-9e59040e7347. The hardware button has alias PBUTTONACTION and GUID 7648efa3-dd9c-4e3e-b566-50f929386280. The start button has alias UIBUTTON_ACTION and GUID a7066653-8d6c-40a8-910e-a1f54b84c7e5. You can check them on the command line with powercfg -aliases.
    Before changing the settings, check which indices they can take on the command line:

    powercfg -q %activeschemeGUID% SUB_BUTTONS PBUTTONACTION
    powercfg -q %activeschemeGUID% SUB_BUTTONS UIBUTTON_ACTION
    

    If that doesn't work you should try with the GUIDs instead of the aliases or try to look up the correct GUIDs/aliases for your configuration. After you've seen which value indices you need, you can change the settings. In my case, disabling the hardware power button was index 0. So changing for respectively AC and DC was:

    powercfg -setacvalueindex %activeschemeGUID% SUB_BUTTONS PBUTTONACTION 000
    powercfg -setdcvalueindex %activeschemeGUID% SUB_BUTTONS PBUTTONACTION 000
    

    I had no log off option for the UIBUTTON_ACTION unfortunatly. So I can't show you that one but it would have been the same principle. That's also the reason why I'm not sure the UIBUTTON_ACTION will do the trick for the start menu.

  2. For the shutdown button in the start menu, I know there is another option in windows 7: change the registry Start_PowerButtonAction located in HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced. I warn you: changing registries is delicate and not really good practice but sometimes it is the only way to change some configurations. I'm not sure but it may also require administrator priviliges so running as administrator may be necessary(can someone confirm this too?).
    These are the hexadecimal values the registry can take:

    Action           Value
    ------------  --------
    Change User        100
    Log Off              1
    Lock               200
    Restart              4
    Sleep               10
    Hibernate           40
    Shutdown             2
    

    You can use regedit GUI to check the current value of the registry. Or check that value on the command line with reg (EDIT: use HKCU instead of HKEY_CURRENT_USER in reg):

    reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v Start_PowerButtonAction
    

    After you checked the registry exists and has the value corresponding to your start menu shutdown button you can modify it. But before modifying any registry, make sure to back-up the registry with regedit (this official Windows link also offers help) in case something goes wrong.
    To modify the registry Start_PowerButtonAction and set "Change User" as action in batch you can use:

    reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v Start_PowerButtonAction /t REG_DWORD /d 100 /f
    

    If it didn't exist you can create it with the same command and try it out. If it doesn't work, use the back-up to restore at least the HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced key.

After you've tested it you can add those commands in a batch script. The final script would look like this:

@echo off
SetLocal

REM Get GUID of current power scheme
FOR /F "tokens=4" %%G IN ('powercfg /getactivescheme') DO set activeschemeGUID=%%G

REM Custom power scheme name
set custom_name=CUSTOM_POWER_SCHEME_STACK

REM Check if it already exists and if it exists, get its GUID
FOR /F "tokens=4" %%G IN ('powercfg -list ^| find "%custom_name%"') DO (
    REM custom power scheme with that name already exists
    set custom_GUID=%%G
    goto :SetCustomActive
)

REM Here we're sure it doesn't exist: copy current active scheme and get GUID of that copy
FOR /F "tokens=4" %%G IN ('powercfg -DUPLICATESCHEME %activeshcemeGUID%') DO et custom_GUID=%%G

REM change the name of the new scheme (the copy) to the custom name
powercfg -CHANGENAME %custom_GUID% %custom_name%

:SetCustomActive
powercfg -SETACTIVE %custom_GUID%
set activeschemeGUID=%custom_GUID%

REM Your code
POWERCFG /CHANGE monitor-timeout-ac 0
POWERCFG /CHANGE disk-timeout-ac 0
POWERCFG /CHANGE standby-timeout-ac 0
POWERCFG /CHANGE hibernate-timeout-ac 0

REM change the power button
powercfg -setacvalueindex %activeschemeGUID% SUB_BUTTONS PBUTTONACTION 000
powercfg -setdcvalueindex %activeschemeGUID% SUB_BUTTONS PBUTTONACTION 000

REM Change the start menu button (replace with powercfg method below if available)
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v Start_PowerButtonAction /t REG_DWORD /d 100
REM powercfg -setacvalueindex %activeschemeGUID% SUB_BUTTONS UIBUTTON_ACTION ???
REM powercfg -setdcvalueindex %activeschemeGUID% SUB_BUTTONS UIBUTTON_ACTION ???

EndLocal
exit /b 0

PS: For the restart you can add the shutdown command at the end of your scrit before the exit:

SHUTDOWN.exe /r /f /t 60 /d P:2:4

will generate a restart after 60 seconds with the reason: "Operating System: Reconfiguration (Planned)". See this link for more info.

PPS: In case you use the registry solution, you can use the following to check if the script is running with administrator rights at the beginning of your script (if admin rights are required):

openfiles > NUL 2>&1 
if %ERRORLEVEL% NEQ 0 (
    REM Command failed => no admin rights
    echo This executable requires admin-rights!
    exit /b 1
)

openfiles is a command that always requires admin rights. If it fails you know the script hasn't been run as an administrator.