Thursday, May 31, 2018

command line - How to "Open CMD Here as Administrator" in right-click menu, with no elevation prompt. Without changing UAC rules

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.

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...