0

I'm going to preface this with that I am relatively new to registry tweaking, but please bear with me.


Today I was messing with my Windows 10 (home, version 1803) and after some googling I did 2 things:

I did some registry tweaks/additions to add the "Open CMD here as administrator" option to right click context menu. This worked perfectly and here are the registry changes/additions I made:


HKEY_CLASSES_ROOT\Directory\shell\OpenElevatedCmd:

(Default) = Open command prompt here as administrator Icon = cmd.exe

HKEY_CLASSES_ROOT\Directory\shell\OpenElevatedCmd\command:

(Default) = PowerShell -windowstyle hidden -Command "Start-Process cmd.exe -ArgumentList '/s,/k,pushd,%V' -Verb RunAs"

HKEY_CLASSES_ROOT\Directory\Background\shell\OpenElevatedCmd:

(Default) = Open command prompt here as administrator

Icon = cmd.exe

HKEY_CLASSES_ROOT\Directory\Background\shell\OpenElevatedCmd\command:

(Default) = PowerShell -windowstyle hidden -Command "Start-Process cmd.exe -ArgumentList '/s,/k,pushd,%V' -Verb RunAs"

HKEY_CLASSES_ROOT\Drive\shell\OpenElevatedCmd:

(Default) = Open command prompt here as administrator

Icon = cmd.exe

HKEY_CLASSES_ROOT\Drive\shell\OpenElevatedCmd\command:

(Default) = PowerShell -windowstyle hidden -Command "Start-Process cmd.exe -ArgumentList '/s,/k,pushd,%V' -Verb RunAs"

HKEY_CLASSES_ROOT\LibraryFolder\background\shell\OpenElevatedCmd:

(Default) = Open command prompt here as administrator

Icon = cmd.exe

HKEY_CLASSES_ROOT\LibraryFolder\background\shell\OpenElevatedCmd\command:

(Default) = (value not Set)


The second thing I did was use task scheduler to make a task that runs admin command prompt without the stupid UAC popup and made a shortcut to it to pin to my taskbar. This worked perfectly as expected. Here is the task I made:

Elevated CMD Scheduled Task General

Elevated CMD Scheduled Task Action

The shortcut target to run this task is: C:\Windows\System32\schtasks.exe /run /tn "Elevated CMD"


It all went downhill when I attempted to freeball it and do something I couldn't find on google.

I thought it would be a good idea to make it so that when I launched the admin command prompt from the context menu in any folder the UAC prompt would not appear. My first thought was to use some sort of method to make the admin command prompt not require the UAC popup, so I did some googling and the only non-third-party method I could find was the one using the "Application Compatibility Toolkit". I tried that method and it didn't work so my next thought was to combine the scheduled task trick with the registry edits that add the "run cmd here as admin" context option. To do this I needed to figure out what to put for the new values in the "command" defaults. So I took the arguments from the working values that added the admin cmd to the context menu(/s /k pushd %V), and I tested them using a command I based off of the working registry values in PowerShell to see if they did what I thought they did:

"Start-Process cmd.exe '/s /k pushd %V'"

This resulted in a new administrator command prompt being opened with a message "the system cannot find the path specified." I assumed this was what should have happened based on how I think those arguments work.

Then I created a new scheduled task called "ecmd" that was identical to my "Elevated CMD" task that launches admin cmd without the annoying UAC prompt, but this time I added the arguments "/s /k pushd %V" to the "Add arguments (optional):" field. I then tested the following in PowerShell:

"Start-Process schtasks.exe '/run /tn ecmd'"

The new arguments "/run /tn ecmd" are the same arguments I used to launch this task with a shortcut. This command worked exactly how I expected, It opened a new admin command prompt without the UAC popup with the same "cannot find specified path" message as my earlier test.

At this point I thought I was good to go, so I changed:

"PowerShell -windowstyle hidden -Command "Start-Process cmd.exe -ArgumentList '/s,/k,pushd,%V' -Verb RunAs""

to:

"PowerShell -windowstyle hidden -Command "Start-Process schtasks.exe -ArgumentList '/run,/tn,ecmd' -Verb RunAs""

In all relevant locations, substituting cmd for schtasks and substituting the original arguments with the arguments that would make task scheduler run the "ecmd" task...

This is where I became completely lost. All that happens when I open the right-click context menu and select "Open command prompt here as administrator" is I get a brief flash of a command window and nothing else.

I have tried these commands as well:

"PowerShell -windowstyle hidden -Command "Start-Process schtasks.exe -ArgumentList '/run,/tn,ecmd' -Verb RunAs""

"PowerShell -windowstyle hidden -Command "Start-Process schtasks.exe -ArgumentList '/run,/tn,ecmd' -Verb Open""

"PowerShell -windowstyle hidden -Command \"Start-Process schtasks.exe -ArgumentList '/run,/tn,ecmd' -Verb RunAs\""

"PowerShell -windowstyle hidden -Command "Start-Process schtasks.exe -ArgumentList '/run,/tn,ecmd'""

"PowerShell -windowstyle hidden -Command "Start-Process schtasks.exe -ArgumentList '/run,/tn,"ecmd"' -Verb RunAs""

"PowerShell -windowstyle hidden -Command "Start-Process schtasks.exe -ArgumentList '/run,/tn,'ecmd'' -Verb RunAs""

"PowerShell -windowstyle hidden -Command "Start-Process schtasks.exe -ArgumentList '/run,/tn"ecmd"' -Verb RunAs""

"PowerShell -windowstyle hidden -Command "Start-Process schtasks.exe -ArgumentList '/run,/tn'ecmd'' -Verb RunAs""

"PowerShell -windowstyle hidden -Command "Start-Process schtasks.exe -ArgumentList '/run,/tn "ecmd"' -Verb RunAs""

"PowerShell -windowstyle hidden -Command "Start-Process schtasks.exe -ArgumentList '/run,/tn 'ecmd'' -Verb RunAs""

All of which produce the same results or some of theme will open a UAC prompt about running task scheduler, and the prompt will list schtasks.exe with the arguments I used "/run" "/tn" and "ecmd" as the location.

I am completely out of ideas and would love to know how to format these commands to make this work. I'm not knowledgeable enough to confidently assert this but I also feel like being able to execute a task with 2 clicks from anywhere in explorer/desktop/library will have a lot of utility in the future when new needs arise.

2 Answers 2

1

I know this is old, but I came across it by chance and figured out a way to do it. It requires a separate batch file, a temporary text file, and it flashes an interstitial cmd window, but in my brief testing it seems to work pretty well.

So save this as the batch file (for example C:\ecmd_here.bat):

@ECHO OFF
SET PATH_FILE=%TMP%\ECMD_PATH
SET /P LOAD_PATH=<%PATH_FILE%

IF "%1" == "SAVE" (
    ECHO SAVING PATH: %2
    ECHO %2 > %PATH_FILE%
    SCHTASKS /RUN /TN ECMD_HERE
)
IF "%1" == "GO" (
    CMD.EXE /S /K PUSHD %LOAD_PATH%
)

Then create the scheduled task to run the batch file, and create registry keys (these next lines need to be run from an admin cmd):

SCHTASKS /CREATE /SC ONCE /ST 00:00 /TN "ECMD_HERE" /TR "C:\ecmd_here.bat GO" /RL HIGHEST
REG ADD HKCR\Directory\Background\shell\ECMD_HERE /d "Open cmd as Admin"
REG ADD HKCR\Directory\Background\shell\ECMD_HERE\COMMAND /d "\"C:\ecmd_here.bat\" SAVE \"%V\""

The way this works:

  • When the batch file is run from the context menu/registry key (in non-admin mode), it saves the current path to a file in the temp folder, then starts the scheduled task, and exits (this is the brief flash).
  • When the scheduled task runs, it launches the batch file (now in admin mode), which reads the temp file to a variable, and starts a cmd at that designated path.

It's a little clunky, and really skirts around the whole UAC thing in a way I don't think MS would like, but it works.

Note: This example just adds the item to the background menu of a directory. I didn't want to add in every menu path for the sake of brevity.

0

Have you already seen this post?

https://stackoverflow.com/questions/20449316/how-add-context-menu-item-to-windows-explorer-for-folders#29769228


Other than that, as I glance over some of the entries you have above, I see that you have some nested single and double quotes. In those scenarios you need to escape the inner single/double quotes (lest your inner quote terminate what you intend to be quotes wrapping a block of content).

Does that make sense?

Here is an example that has both nested single and double quotes at the same time.

"PowerShell -windowstyle hidden -Command \"Start-Process schtasks.exe -ArgumentList '/run,/tn \'ecmd\'' -Verb RunAs\""
1
  • @Guy_Hoozdis The post didn't really have anything I could use. I've spent a couple days trying to solve this. It appears there's no combination of scheduled tasks, powershell, or cmd commands that can do what I want to do (Launch admin cmd "here" with right click menu, with no UAC elevation prompt, without changing any UAC settings that would affect more than just the cmd). At this point I've given up and just used local group policy editor to turn off all UAC elevation prompts. :( Mar 2, 2019 at 4:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .