[Solved] How to make Command Prompt always open as Administrator ?

Discussion in 'Scripting' started by ceo54, Mar 15, 2021.

  1. ceo54

    ceo54 MDL Addicted

    Aug 13, 2015
    867
    361
    30
    Hi,

    I have this strange bug. On one of the systems command prompt always runs elevated but on the other system it doesn't even though the settings in the compatibility tab is there just like the first system in both cmd.exe and it's Start Menu shortcut.

    [​IMG]

    [​IMG]

    The settings are exact same on both systems but on one system they work while on the other they don't.

    Is there any other setting somewhere else that effects the behaviour if terminal opens elevated or just as a normal user ?

    Please if you have any idea, I shell be highly obliged.

    Thank you.

    Edit: The first screenshot is of cmd.exe in System32 folder and the second one is of the Shortcut in Userprofiles.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. Tiger-1

    Tiger-1 MDL Guru

    Oct 18, 2014
    7,897
    10,733
    240
    hi bro right click with on mouse button on the CMD icon, it will open a screen click properties then click on advanced and then click execute as administrator ...simple
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. tnx

    tnx MDL Expert

    Sep 2, 2008
    1,695
    267
    60
    I use a simple REG edit to add "run Command prompt as Admin" to the Context menu.

    here is my example, you could use it as you wish but change the paths to suit you better.

    Reg file
    Code:
    Windows Registry Editor Version 5.00
    
    :: Command Promt-Admin On The Context Menu
    [HKEY_CLASSES_ROOT\Directory\Background\shell\Command Promt Admin]
    "icon"="C:\\Windows\\MyFiles\\Icons\\Command Prompt.ico"
    
    [HKEY_CLASSES_ROOT\Directory\Background\shell\Command Promt Admin\command]
    @="WScript C:\\Windows\\MyFiles\\ContextMenu\\RunCommandPromptasAdmin.vbs"
    :
    :
    This points to my .vbs file which reads as

    Code:
    Set oShell = CreateObject("Shell.Application")
    oShell.ShellExecute "cmd.exe", , , "runas", 1
    It works nicely for me..

    I have this set up in a $OEM$ set up so it is ready at first boot up for me.
     
  4. ceo54

    ceo54 MDL Addicted

    Aug 13, 2015
    867
    361
    30
    #4 ceo54, Mar 15, 2021
    Last edited: Mar 15, 2021
    (OP)
    Thanks for the reaction Tiger but the issue is I'm very bad with command prompt and I have myriad batch files here and there I run. I find it tedious to navigate it through the terminal even if I remember the commands in the first place. This is about holding shift and right clicking to open the command prompt where the executable or batch file is but I need to run cmd as admin to be able to do this.

    Now here's the interesting part. I investigated into it and found the reg keys responsible to elevate the programs are located, I added command prompt there. It works on the first machine but still doesn't work on the second.

    Code:
    [HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers]
    "C:\\Windows\\System32\\cmd.exe"="RUNASADMIN"
    
    [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers]
    "C:\\Windows\\System32\\cmd.exe"="RUNASADMIN"
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. ceo54

    ceo54 MDL Addicted

    Aug 13, 2015
    867
    361
    30
    Thanks but I already have two instances of open command prompt here, they just doesn't open elevated. I suppose adding this will add another instance in the context menu. I will look for the solution should I not find one, I will go this route. Many thanks!
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. Tiger-1

    Tiger-1 MDL Guru

    Oct 18, 2014
    7,897
    10,733
    240
    hmm then run sfc /scannow and verify if all ok
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. tnx

    tnx MDL Expert

    Sep 2, 2008
    1,695
    267
    60
    Another way..

    save the following as "RunCommandPromptasAdmin.vbs"

    Code:
    Set oShell = CreateObject("Shell.Application")
    oShell.ShellExecute "cmd.exe", , , "runas", 1
    Place this file in "Windows"

    make a new shortcut, say on the desktop, edit the "target" to
    this will run the command promt as Admin. You could save the shortcut in a folder somewhere and Pin it to the Start Menu or Taskbar for easy access.
     
  8. ceo54

    ceo54 MDL Addicted

    Aug 13, 2015
    867
    361
    30
    Corrupt files found on both systems. They are just identical. Another interesting part is other 3rd party programs that run elevated are running fine on both machines.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  9. ceo54

    ceo54 MDL Addicted

    Aug 13, 2015
    867
    361
    30
    This one is even better I'll save this for future use. Thanks again.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  10. ceo54

    ceo54 MDL Addicted

    Aug 13, 2015
    867
    361
    30
    Resolved it. Problem was the layer files in Windows\appatch folder corresponding to Application Experience service which the other system was missing. Restored the files, merged the registry and it started working fine. Many thanks to @Tiger-1 and @tnx for helping me out here. Your valuable help is highly appreciated guys. Cheers!
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  11. if you reg tweak cmd to always run as admin it will leave you in a major security flaw on your OS so that any MITM will succeed on the go by an attacker & will left you with many other security vulmerabilities etc etc. Pl think about meterpreter session , Stub , In Secure flaws , vulnerabilities & its security patching.