John Robbins' Blog

Elevate a Process at the Command Line in Vista

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.

On Mar 26 2007 8:56 PMBy jrobbins With 26 Comments

Comments (26)

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

  2. 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.

  3. 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$.

  4. 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

  5. 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\

  6. 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

  7. Your elevate tool for command line is quite nice, but for cases where you need to set a working directory or see the ouptut I've got a solution.

    I've got tools for this purpose that you can get from my site (kzorro.webs.com). When you use filedir2prompt in combination with Compatibility Sword, you can easily run a console application/batch file elevated and be able to see the output. I've wrote up a tutorial on creating a shortcut or CSword profile to acomplish this task which you can read up on here:

    http://kzorro.webs.com/apps/blog/show/1350937-windows-uac-console-applications-get-one-click-access-to-output-from-administrative-console-applications-using-csword-and-filedir2prompt

  8. John,

    I know this is in your archives but I had to write in. What a wonderful tool. I can think of tons of ideas where this can be useful. Thank for you the open source too.

    Hey weird question for you. Sometimes people get weird about modifying and using the software even if it is open source so I wanted to ask first. I was trying to find the licensing for this but couldn't. Since it is open source, is this licensed under the GNU Public license where we can change it, modify it, give it to other people that can use it, etc etc etc without getting into a possible legal battle or is true freeware with no license and it just here do what you want with it software? I know a ton of people that can use this that I can give this to. I just wanted to ask first before I start telling everyone about this.

    This program is a 1000 times better than changing the registry entries every time for a program to elevate here and having to right click. I love the command line. With Vista, it completely took it away if you wanted to utilize UAC other than right clicking and running as administrator. This program has brought it back. :)

    Great job and what an excellent find. I now have your website in my favorites.

    Wayne Leiser, CTO
    Spectacular Computer Repair
    www.scrfix.com
    941.923.6280
    Mon - Thu 11am - 7pm EST
    Fri - Sat 11am - 3pm EST

  9. Wayne,

    Glad you liked Elevate. As for a license, how about this: I don't really care what you do with it. :) It's so simple there's no need worrying about it. Please feel free to share with as many people as you want. I guess out of courtesy you could refer people to my blog.

    I can't believe this wasn't part of Vista and higher either!

    - John Robbins

  10. John,

    Thanks and I agree with you. I appreciate you putting this together. I am glad I found this. I will post a link to your blog in my forums. Please be advised that I do not get a lot of people to my forums, Mainly just computer repair technicians looking on how to repair something because I psot all of the hard to find repairs online for my own technicians.

    Wayne

  11. Ok don't mean to take attention away from the elevate tool posted here but I'd think it be only fair if I update with my post above.

    FileDir2Prompt as mentioned above in that long tutorial I had up can now elevate and keep output up for display/interaction without having to use a second program.

    How to use in a batch/script file or from command line?

    filedir2prompt /adminapp

    You may also do the same thing using the GUI.

    As for Compatibility Sword I suppose I should have posted an example of what you would put in a batch file/script. You can accomplish the xcopy command shown above in a batch file/script with the following:

    csword /app="c:\windows\system32\xcopy.exe" /param="c:\source c:\destination"

  12. Is there a way to elevate the rights of the program? I have one program on a 2008 R2 server that runs fine with administrator rights but not as a user. If I have to give all the users an admin name and password to run the program, I might as well set them up as local admins!
    Can't you set up a program just to use elevated rights?

  13. BigMac,

    Yes you can. Create a manifest file that sits next to the .EXE that sets the autoelevate flag.

    - John Robbins

  14. hey im using gygan its a file sharing applacation thats still in beta. how do i elavate it? downloaded the link and now what?

  15. This is great! I combined it with shell registering and unregistering of DLLs from http://forums.webhostautomation.com/showthread.php?t=7851&page=1. So good!

  16. John, I just found a bug in the elevate program. When utilizing it to send arguments to a secondary batch file, it doesn't handle spaces. It crashes the batch file.

    See: http://forums.techguy.org/dos-other/1007711-solved-sending-variables-crashes-new.html

    When using start to send the variables it works just fine. When utilizing elevate it crashes. After much troubleshooting this was found to be due to the spaces.

    Are you planning on coming out with a version that handles spaces?

    Wayne

  17. As a matter of fact, currently in windows 7 when utilizing elevate to transfer arguments between batch files such as elevate test.bat
    :: The elevate program cannot handle commas, spaces and \ therefore we need to work around this by changing these to characters that can be handled by elevate.
    :: Change , to $
    :: Change space to _
    :: Change \ to '
    So to work around this we have to change it to characters that Elevate will transfer without causing the new batch window to crash. If this does get repaired with a new version I would love some other options in there such as what the start command has within Windows 7.

  18. Wayne,

    I looked at your bug report. Elevate is processing the command line options correctly for a program. However, cmd.exe has all sorts of weird command line parsing that I didn't think was necessary in such a simple tool. Elevate comes with source so please feel free to change it to meet your needs.

    - John Robbins

  19. Excuse me but in the command don't I nust to write the name and password of the administrator?

  20. Sorry. Can I write all in a batch file dos and run by an icon?

    PS: excuse me for my english, I'm italian.

  21. Look I need to know exactly what to insert into a notepad converted to a batch file to open an elevated cmd prompt. Can Anyone help me with this? If so, please email me at tjmikha@gmail.com. My C:/ drive is unwritable being that it is controlled by a group policy But I know there's a way around this all by having the elevated cmd doing netuser and making my own admin account. I'd like someone to please help.

Leave a Comment

Archives

Tags