Dynamically change icon in context menu

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
MarioZac
Posts: 38
Joined: 16 May 2018 00:12

Re: Dynamically change icon in context menu

#31 Post by MarioZac » 30 May 2018 20:58

Thanks for your input. Solution complexity should correspond to the task at hand, and switching Current Display sounds straightforward at user level. For now I'd leave it "as is", it works OK though not ideal. Will see if someone can come up with a native Windows Shell solution. Its interesting in general, as many people would benefit from learning how to speed up one's workflow by modifying Context Menu with simple but clever means. :wink: Not much about it on the web except basics.

sambul36
Posts: 7
Joined: 11 Nov 2020 20:09

Re: Dynamically change icon in context menu

#32 Post by sambul36 » 29 May 2022 22:19

Can someone point why the above batch PlanIcon.bat worked well in Win 10 64bit, but doesn't work in Win 11 64bit?

Code: Select all

@echo off
:: The batch updates Active Power Plan icon in Desktop Context Menu, when Power Plan is changed. Can run as a scheduled task triggered by EventID 12 "Power Plan changed"
setlocal EnableExtensions EnableDelayedExpansion

set "key=HKCR\DesktopBackground\Shell\Switch Power Plan"
set "plan1=Balanced" & set "plan2=High performance" & set "plan3=Power saver"
set "icon1=powercpl.dll,0" & set "icon2=powercpl.dll,2" & set "icon3=powercpl.dll,1"

for /f "tokens=5,6" %%a in ('powercfg -getactivescheme') do ( set pln1=%%a & set pln2=%%b
	if defined pln2 ( set "pln=!pln1:~1!!pln2:~0,-1!" ) else ( set "pln=!pln1:~1,-2!" ))

for %%d in (1 2 3) do ( if !plan%%d!==!pln! ( reg add "%key%" /v Icon /d !icon%%d! /f > nul ))
endlocal
exit /b
Last edited by sambul36 on 30 May 2022 16:20, edited 1 time in total.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Dynamically change icon in context menu

#33 Post by penpen » 30 May 2022 03:24

I can't check myself (since i I have no Win11), So i can list only two potential issues i see ad hoc:
  1. Microsoft might have changed the GUID of the power plans.
    You could compare the GUID used in "ChangePowerPlan.reg" with the GUID listed by following command:

    Code: Select all

    powercfg.exe /L
    If there was a change, then change the "ChangePowerPlan.reg" accordingly and import the new values to the registry (again).
  2. Microsoft might have changed the string representation of the active power scheme.
    You could check the output of the following command:

    Code: Select all

    powercfg -getactivescheme
    If there was a change, then change the for/F-loop in the batch accordingly.

penpen

sambul36
Posts: 7
Joined: 11 Nov 2020 20:09

Re: Dynamically change icon in context menu

#34 Post by sambul36 » 30 May 2022 13:03

Thanks for the reply. I checked it out, and apparently these strings remained the same (see screenshot). Further, I can change the powerplan from Context Menu without errors, only changing the relevant icon by the scheduled task gives an error. However, the Task finishes without any errors in the Event Viewer log, but execution of the batch itself gives the unspecified error.

As a side matter, the above .reg file is unable to add the keys to Win 11 HKCU while running without errors, so instead I added the same keys to HKLM by running a corrected .reg file, but it did not affect changing the powerplan from Context Menu, so its OK. While the icon value is supposed to be changed in HKCR, it is added there, but not changed according to current powerplan each time the batch is run. I also tried running the batch from Win 11 C:\ root without success. But it works perfect in Win 10.
Attachments
PlanIcon.bat_Error.jpg
PlanIcon.bat_Error.jpg (24.22 KiB) Viewed 6092 times
Compare Win 10_11 powerplans.jpg
Compare Win 10_11 powerplans.jpg (154.49 KiB) Viewed 6092 times
Last edited by sambul36 on 30 May 2022 13:16, edited 4 times in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Dynamically change icon in context menu

#35 Post by aGerman » 30 May 2022 13:13

Microsoft fundamentally changed the appearance of the context menu of Win 11.
If you want to turn back to the the classic context menu, import this setting and reboot:

classic_context_menu.reg

Code: Select all

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Classes\CLSID\{86ca1aa0-34aa-4e8b-a509-50c905bae2a2}\InprocServer32]
@=""
No guarantee that the power config items are going to appear though. I didn't test it.

Steffen

sambul36
Posts: 7
Joined: 11 Nov 2020 20:09

Re: Dynamically change icon in context menu

#36 Post by sambul36 » 30 May 2022 13:19

Thanks for the suggestion, I already did it to have faster access to main Context menu. The Change Power Plan menu does appear and work the same way in Win 11 as in Win 10 now, except the above icon batch gives unspecified execution error. I wonder if someone is able to test it on a Win 11 64bit US English PC?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Dynamically change icon in context menu

#37 Post by aGerman » 31 May 2022 09:32

Maybe try to use the full path to the icon.

Code: Select all

for %%d in (1 2 3) do ( if !plan%%d!==!pln! ( reg add "%key%" /v "Icon" /t REG_EXPAND_SZ /d "%%SystemRoot%%\System32\!icon%%d!" /f > nul ))
Steffen

sambul36
Posts: 7
Joined: 11 Nov 2020 20:09

Re: Dynamically change icon in context menu

#38 Post by sambul36 » 01 Jun 2022 07:41

aGerman wrote:
31 May 2022 09:32
full path
Yes, it works. :) Also works this way, and in fact that type name should be in the original batch code for completeness:

Code: Select all

for %%d in (1 2 3) do ( if !plan%%d!==!pln! ( reg add "%key%" /v "Icon" /t REG_EXPAND_SZ /d !icon%%d! /f > nul ))
But why the above .reg file can't add its keys to Win 11 HKCU, only to HKLM upon destination correction?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Dynamically change icon in context menu

#39 Post by aGerman » 01 Jun 2022 09:46

IDK. Just tried it and it's been imported w/o problems.
regedit.jpg
regedit.jpg (80.49 KiB) Viewed 6030 times
Steffen

Post Reply