How to remove delay in elevated priveleges conext menu script?

Discussion in 'Windows 10' started by OpenSourceGhost, Feb 23, 2022.

  1. OpenSourceGhost

    OpenSourceGhost MDL Member

    Feb 14, 2022
    170
    24
    10
    I use the following registry tweak to create context menu options that open CMD and PowerShell with elevated priveleges, but selecting either option starts CMD/PowerShell, then restarts it, which creates a delay that I wish not be there:
    Code:
    [HKEY_CLASSES_ROOT\Directory\shell\OpenCmdHereAsAdmin]
    @="Elevated Command Prompt"
    "Extended"=-
    "Icon"="imageres.dll,-5324"
    
    [HKEY_CLASSES_ROOT\Directory\shell\OpenCmdHereAsAdmin\command]
    @="cmd /c echo|set/p=\"%L\"|powershell -NoP -W 1 -NonI -NoL \"SaPs 'cmd' -Args '/c \"\"\"cd /d',$([char]34+$Input+[char]34),'^&^& start /b cmd.exe\"\"\"' -Verb RunAs\""
    
    [HKEY_CLASSES_ROOT\Directory\Background\shell\OpenCmdHereAsAdmin]
    @="Elevated Command Prompt"
    "Extended"=-
    "Icon"="imageres.dll,-5324"
    
    [HKEY_CLASSES_ROOT\Directory\Background\shell\OpenCmdHereAsAdmin\command]
    @="cmd /c echo|set/p=\"%V\"|powershell -NoP -W 1 -NonI -NoL \"SaPs 'cmd' -Args '/c \"\"\"cd /d',$([char]34+$Input+[char]34),'^&^& start /b cmd.exe\"\"\"' -Verb RunAs\""
    
    [HKEY_CLASSES_ROOT\Drive\shell\OpenCmdHereAsAdmin]
    @="Elevated Command Prompt"
    "Extended"=-
    "Icon"="imageres.dll,-5324"
    
    [HKEY_CLASSES_ROOT\Drive\shell\OpenCmdHereAsAdmin\command]
    @="cmd /c echo|set/p=\"%L\"|powershell -NoP -W 1 -NonI -NoL \"SaPs 'cmd' -Args '/c \"\"\"cd /d',$([char]34+$Input+[char]34),'^&^& start /b cmd.exe\"\"\"' -Verb RunAs\""
    
    [-HKEY_CLASSES_ROOT\LibraryFolder\background\shell\OpenCmdHereAsAdmin]
    
    ; To allow mapped drives to be available in command prompt
    [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System]
    "EnableLinkedConnections"=dword:00000001
    I used to use a different registry tweak that provided the same conext menu options, but didn't create any delays, just opened CMD with elevated priveleges instantly.
     
  2. AveYo

    AveYo MDL Expert

    Feb 10, 2009
    1,836
    5,730
    60
    Obviously your code goes through cmd, powershell, then starts yet another cmd process.
    Using parameters to set it to run on background won't ever work across standard/elevated barrier, just like passing parameters does not.
    There's only one built-in way to do what you want, and it requires the dedicated verb "runas" and the "HasLUAShield" configuration, like this:

    Code:
    Windows Registry Editor Version 5.00
    
    [-HKEY_CLASSES_ROOT\Directory\Background\shell\OpenCmdHereAsAdmin]
    [-HKEY_CLASSES_ROOT\Directory\shell\OpenCmdHereAsAdmin]
    [-HKEY_CLASSES_ROOT\Drive\shell\OpenCmdHereAsAdmin]
    [-HKEY_CLASSES_ROOT\Folder\shell\OpenCmdHereAsAdmin]
    
    [HKEY_CLASSES_ROOT\Directory\background\shell\runas]
    "MuiVerb"="Elevated Command Prompt"
    "Icon"="imageres.dll,-5324"
    "Position"="Middle"
    "NoWorkingDirectory"=""
    "HasLUAShield"=""
    [HKEY_CLASSES_ROOT\Directory\background\shell\runas\command]
    @="C:\\Windows\\System32\\cmd.exe /k pushd \"%W\""
    
    [HKEY_CLASSES_ROOT\Drive\shell\runas]
    "MuiVerb"="Elevated Command Prompt"
    "Icon"="imageres.dll,-5324"
    "Position"="Middle"
    "NoWorkingDirectory"=""
    "HasLUAShield"=""
    [HKEY_CLASSES_ROOT\Drive\shell\runas\command]
    @="C:\\Windows\\System32\\cmd.exe /k pushd \"%L\""
    
    ;[-HKEY_CLASSES_ROOT\Directory\background\shell\runas]
    ;[-HKEY_CLASSES_ROOT\Drive\shell\runas]
    
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. OpenSourceGhost

    OpenSourceGhost MDL Member

    Feb 14, 2022
    170
    24
    10
    #3 OpenSourceGhost, Feb 24, 2022
    Last edited: Feb 24, 2022
    (OP)
    Thanks!

    It isn't my script, but the one from TenForums. I can't post full link due MDL newbie user limitation, but the link ends in "59686-open-command-window-here-administrator-add-windows-10-a.html" .

    I don't know nearly enough to interpret what you're saying... I guess you're saying the script in my OP isn't fully functional? Again, I have no idea...

    Could you please provide a similar script, but for PowerShell? The one I'm using is from (TenForums) 60177-add-open-powershell-window-here-administrator-windows-10-a.html .
     
  4. AveYo

    AveYo MDL Expert

    Feb 10, 2009
    1,836
    5,730
    60
    If you want to have both CMD and Powershell and without delay, it's not possible as I've explained - there's only one runas entry.
    No point in having both either, since it's just so trivial to write powershell in cmd and cmd in powershell, respectively.
    So, the powershell alternative:
    Code:
    Windows Registry Editor Version 5.00
    
    [HKEY_CLASSES_ROOT\Directory\background\shell\runas]
    "MuiVerb"="Elevated PowerShell"
    "Icon"="powershell.exe,0"
    "Position"="Middle"
    "NoWorkingDirectory"=""
    "HasLUAShield"=""
    [HKEY_CLASSES_ROOT\Directory\background\shell\runas\command]
    @="C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -nop -noe -c $A=([environment]::commandline-split'-[-]%%+ ?',2)[1]; set-location -lit $A; # --%% %W"
    ; special-characters-friendly argument parsing by AveYo
    
    [HKEY_CLASSES_ROOT\Drive\shell\runas]
    "MuiVerb"="Elevated PowerShell"
    "Icon"="powershell.exe,0"
    "Position"="Middle"
    "NoWorkingDirectory"=""
    "HasLUAShield"=""
    [HKEY_CLASSES_ROOT\Drive\shell\runas\command]
    @="C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -nop -noe -c set-location -lit '%L'"
    ; special-characters-friendly argument parsing by AveYo
    
    ;[-HKEY_CLASSES_ROOT\Directory\background\shell\runas]
    ;[-HKEY_CLASSES_ROOT\Drive\shell\runas]
    
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. OpenSourceGhost

    OpenSourceGhost MDL Member

    Feb 14, 2022
    170
    24
    10
    You lost me there... The script in the OP has a delay whenever I try to start CMD, but the one you provide does not, yet both allow me to start CMD as Admin (Elevated Priveleges) via right-click context menu.

    Is that because the CMD context menu script involves launching PowerShell? Why does it do it in the first place if it isn't necessary and doesn't add PowerShell context menu?

    Anyway, your scripts make me happy - that's the most important part :D. THANK YOU!

    EDIT: Hmm.. Your PowerShell script simply replaces CMD context menu option with PowerShell option, but doesn't add it to the existing CMD context menu option. Is that because CMD is basically the same thing as PowerShell? AFAIK, PowerShell can perform some exclusive commands like CMD can't, such as removal of UWP Apps, but maybe not...
     
  6. AveYo

    AveYo MDL Expert

    Feb 10, 2009
    1,836
    5,730
    60
    Maybe you've missed it, here it is in bold.
    I would just use the PowerShell one, and whenever needing cmd, just enter cmd in powershell..
    Else, you're gonna have to pick one of the two to be used with the delay you've complained in OP
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. AveYo

    AveYo MDL Expert

    Feb 10, 2009
    1,836
    5,730
    60
    #8 AveYo, Feb 25, 2022
    Last edited: Feb 25, 2022
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. javaspain

    javaspain MDL Member

    Jul 29, 2009
    218
    114
    10
    simple, install the latest Powershell from G I T H U B . . .

    upload_2022-2-25_21-55-25.png
     
  9. AveYo

    AveYo MDL Expert

    Feb 10, 2009
    1,836
    5,730
    60
    "Just" use --[Insert 3rd party binary]-- vs. make a 5-line plain-text reg file and double-click it. Are you serious? Why not --[Build your own shell extension]-- then. Or --[Use Arch]--
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  10. acer-5100

    acer-5100 MDL Guru

    Dec 8, 2018
    4,003
    2,923
    150
    #13 acer-5100, Feb 26, 2022
    Last edited: Feb 26, 2022

    @BAU
    Aside that other options were already given, so no need to reinvent the wheel on that side, I took the chance to suggest something really really worth using.

    Something that simplifies the task of launching a cmd shell w/o the need to launch stupid PS things before.

    Sometimes the "I like to learn" part is just overruled by the "just works" part.
     
  11. Dark Dinosaur

    Dark Dinosaur X Æ A-12

    Feb 2, 2011
    4,172
    5,980
    150
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  12. AveYo

    AveYo MDL Expert

    Feb 10, 2009
    1,836
    5,730
    60
    #15 AveYo, Feb 26, 2022
    Last edited: Feb 26, 2022
    edit because we posted at the same time

    it might be great, worth using. was not single-ing you out.
    op had a working reg solution. it just did not like how it worked. at no point did he request a c++ or said anything-goes ;)

    just NO. never-ever provide a "solution" that gets rid of a mild-annoyance by completely removing a security feature. it's enough microsoft does not care enough about UAC. but to disable it completely is kinda irresponsible. op wants cmd / ps to run as admin. it is implied it does not want everything on it's pc to run as admin as well, else would have asked for that..
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...