Wintellect Blogs

Elevate a Process at the Command Line in Vista

27 Mar , 2007  

Vista is quite nice in keeping you from running too many things at elevated rights so you don’t get into trouble. Where things breakdown are when you want to start a particular process with elevated rights from a command line. The RUNAS program does not have any switch to run a program with your user credentials, but let you bump the rights. In addition, if you use RUNAS to start a process that requires elevation, you’ll just get an error 740 saying “The requested operation requires elevation.” For us old command line jockey’s this is not a satisfying situation.

If you want a program to always run with elevated privileges, you can always right click on it in Explorer, select Properties and in the Compatibility tab, tell the operating system you want to always run this program as an administrator.

The only problem is that there are numerous programs you don’t want to use the “always elevate” sledgehammer on permanently. For example, I don’t run Visual Studio elevated, but there are times when I need to use it elevated. While I could start a separate console with elevated rights, switch to it, and execute the program, that’s strikes me as a far too much work when I can write a program instead! Thus Elevate was born. Just execute “elevate notepad” and you’ll be prompted with the UAC prompt to run notepad right from the command line.

Elevate will take numerous command line arguments to make your command life a little bit easier and to potentially be used in batch or script files. Here’s the help from the program:

Elevate 4.0.10326.0
(c) 2007 – John Robbins – www.wintellect.com

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

Note that because the way ShellExecute works, Elevate cannot set the
current directory for prog. Consequently, relative paths as args will
probably not work.

As you can see, Elevate works with %COMSPEC% so my beloved 4NT works as well. Down load the source from here.

The magic that Elevete uses? Passing “runas” as the verb to ShellExecute.


  • http://itsvista.com/2007/03/links-for-2007-03-28/ Anonymous
  • Anonymous

    Sound awesome. How about a compiled version for us non-.net folk?

  • Anonymous

    tantalizing but useless to the sysadmins of the world who don’t have time or resources to compile it.

  • jrobbins

    All,

    Sorry! I fixed the .ZIP to include the .EXE.

    By the way, if you have the .NET Framework installed, you have MSBUILD.EXE and the compilers. In a command window, switch to the source code and execute “MSBUILD foo.sln” to compile the solution.

    -John.

  • Anonymous

    The Shell execute works exactly as you write in comment, but it’s easy to change folder to the absolute path where is required application and then Shell execute and after change it back :-) just my comment … anyway it’s bad that so important functionality is not implemented streight by M$.

  • Anonymous

    Can you advise me please. I am trying to use elevate on winodws 7 beta to do a xcopy

    elevate xcopy c:\source\ C:\destination\

    but all that I get is a shell quickly opening and then closing with no prompts. If I do elevate notepad it opens but no UAC prompts

    regards Rick

  • Anonymous

    Rick,

    Maybe it would work if you launch an elevated command interpreter that then ran xcopy?

    ie.
    elevate cmd.exe /c xcopy c:\source\ C:\destination\

  • jrobbins

    Thanks Alun,

    Yes, if you want to execute command line commands it’s best to elevate CMD.EXE or PowerShell.exe and do the command from there.

    – John Robbins

  • http://www.jquirke.com.au Anonymous

    Terrific, exactly what I have been looking for. Cheers mate.