1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

SUDO for CMD

Discussion in 'CMD' started by MaartenG, Mar 17, 2017.

  1. MaartenG

    Joined:
    Aug 3, 2016
    Messages:
    355
    Likes Received:
    8
    Yesterday someone asked me if I knew a Windows built-in way to have some sort of SUDO to elevate from the CMD prompt, so you could do things like sudo regedit, sudo xxx.cpl, sudo xxx.mmc or even sudo del abc.txt.
    I didn't and as it was a slow day, we decided to team up: he would google for it and I would try to come up with something. Google didn't give us anything (other than external utilities and a lot of people asking the same question), but I did find a way.
    It was still a little rough around the edges (to my taste), but he was happy.

    I'm considering posting it here, but as it requires more work on it, translation to English and it is quite off-topic here (in TCC you could just do something like alias sudo=start /elevated /pgm; Powershell is trivial too). Beside all that: the general interest in the other scripts I penned down here are downloaded in single digits numbers, so there might be no interest at all in this one.

    Before going through all the effort: is there any interest in a SUDO for CMD?
     
    #1 MaartenG, Mar 17, 2017
    Last edited: Mar 17, 2017
  2. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,188
    Likes Received:
    11
    I haven't tried this, but it looks like it might work:
    sudo.cmd
    Code:
    @echo Set objShell = CreateObject("Shell.Application") > %temp%\sudo.tmp.vbs
    @echo args = Right("%*", (Len("%*") - Len("%1"))) >> %temp%\sudo.tmp.vbs
    @echo objShell.ShellExecute "%1", args, "", "runas" >> %temp%\sudo.tmp.vbs
    @cscript //NoLogo %temp%\sudo.tmp.vbs
    
    You'd use it like: sudo command args

    And I guess this registry hack turns off UAC prompts:
    Code:
    Windows Registry Editor Version 5.00
    [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System]
    "ConsentPromptBehaviorAdmin"=dword:00000000
    
     
  3. MaartenG

    Joined:
    Aug 3, 2016
    Messages:
    355
    Likes Received:
    8
    Nice one! Never knew you could use the RunAs verb with ShellExecute. You learn something new every day.... Thanks!
    I did test it and it works great for the regular stuff: start cpl's /mmc's or start an elevated cmd /regedit (which is probably enough for most cases).

    Just FYI: the elevated command starts in System32 (although I tried to convice ShellExecute to use a different working directory by passing that as an argument) and it is allergic to double quotes. You can't do something like: sudo "notepad".
    Still: very nice!

    ( FWIW: the CMD equivalent of %@UNQUOTES[%1] is %~1 )
     
  4. w_krieger

    Joined:
    Nov 2, 2008
    Messages:
    176
    Likes Received:
    0
    I've been using a program called 'elevate'. It elevates the named program, both under cmd and tcc.
    Download at [title]

    Another program i found is ELE, from winreview.ru This launches COMSPEC at the command directory, in a separate window.
    [title] and select the download (Ckayati) link.
     

Share This Page