Launch elevated processes from the command line

Posted on

Every now and then you need to run an elevated command from the command line. If the application always requires elevation (i.e. the binary has been marked as requireAdministrator), the UAC prompt shows up – but in the case the application supports both non-elevated and elevated usage and you explicitly want it to run elevated, there is little support for you.

I would have expected to have been augmented by something like a /elevate-switch, but unfortunately, this is not the case and you are left with having to open a new elevated command prompt to continue.

Fortunately, a tool to fill in this gap is straightforward and indeed J. Robbins has already created one. However, the tool is written in C# – it might be old-fashioned, but I still prefer those little tools to be unmanaged and free of the .Net-overhead. I thus created elevate.exe, which provides the same features as John’s tool but is written in C. In fact, it accepts exactly the same command line parameters, which are:

    Execute a process on the command line with elevated rights on Vista
    Usage: Elevate [-?|-wait|-k] prog [args]
    -?    - Shows this help
    -wait - Waits until prog terminates
    -k    - Starts the the %COMSPEC% environment variable value and
                    executes prog in it (CMD.EXE, 4NT.EXE, etc.)
    prog  - The program to execute
    args  - Optional command line arguments to prog


    elevate ipconfig -registerdns
    elevate -wait procexp
    elevate -k dir

Having this tool at hand, it is now easy to also extend the shell context menu for directories by an ‘Open Elevated Console here’ entry by adding the following registry entries:

    [HKEY_CLASSES_ROOT\Directory\shell\Open Elevated Console here]
    @="Open Ele&vated Console here"
               Open Elevated Console here\command]
              /K \"title %1 && color 1a && cd /D %1\""

Download source code, x86 and x64 binary.

Update: You may use the source code under the terms of the MIT License.

**Update 2: **The source code is now available on Github

Any opinions expressed on this blog are Johannes' own. Refer to the respective vendor’s product documentation for authoritative information.
« Back to home