Vista Tip: Elevated Command Prompt Here

One of the things that irks me is I’m trying to do stuff in a deeply buried directory in Explorer and the realize that I want to do something Command Prompt related. You get all the way down to where you want to be and say “uwaaaah!” because you realize that you now have to type that long path into a command window. Sure you could copy the path from the directory bar and paste in the command window, but, well, that’s a bunch of steps I don’t feel like dealing with.

In Vista, there’s an addition challenge because you might want a Command Prompt with Elevated permissions.

Below you’ll find a set of Registry settings that will enable a “Command Prompt here” and “Elevated Command Prompt here” menu item when you right click on a drive or directory in Explorer, including the Start Menu Vista Button.

First, the obligatory “if you screw up your computer by using this Registry setting, you can’t blame me”. Having said that I’ve got it running on four machines without any ill side effects, so I’m content to say that this is “good to go”.

Second, the obligatory history. Command Prompt here is the name of Power Toy for Windows XP. Works fine for XP, but not so much on 2003 or Vista. In fact, it will work – the Power Toy simply adds some keys to the registry – but the installer gets cranky. Since I use Windows XP, Windows 2003, and Windows Vista, I wanted something that would work on all three.

For what it’s worth, Vista already has Command Prompt here in the menu but it’s normally hidden. It comes down to a single REG_SZ value called Extended, which is removed as part of the Registry file below. If you don’t want to lose the hidden option, remove these two lines. I want it to always be there, so I delete that REG_SZ value.

However, that doesn’t address the problem of Elevation. I first thought “wouldn’t it be nice if there was sudo command” but I haven’t found one yet. Then I found another registry option: runas. Adding this to the Registry settings adds another item on the Explorer context menu that allows you to open a elevated Command Prompt. If you only want it to show on a Shift + right-click, add a new REG_SZ value named Extended (and with no value set).

Also, because all of these use pushd rather than cd this will work across drives.

How do I get this for my Vista machine! Well, I don’t like the idea of having REG files for people to download: too many people that shouldn’t have access to REG files will and I don’t want to encourage that. It’s bad enough people willing bid on eBay auctions like “ZOMG! PS4!” and then complain that they got two PS2’s taped together – and those people had to not only bid but send money through PayPal… so for this, I’ll give you the contents of the REG file: you copy if from here, paste it into a file named PromptHere.reg, and merge that into your registry. If you don’t know what I just said – and you’re still reading this – ignore this post! And of course, you can always edit the registry by hand.


Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\shell\cmd]

@="Comm&and Prompt here"

"NoWorkingDirectory"=""

"Extended"=-

[HKEY_CLASSES_ROOT\Directory\shell\cmd\command]

@="cmd.exe /k \"pushd %L && title Command Prompt\""

[HKEY_CLASSES_ROOT\Directory\shell\runas]

@="Ele&vated Comm&and Prompt here"

"NoWorkingDirectory"=""

[HKEY_CLASSES_ROOT\Directory\shell\runas\command]

@="cmd.exe /k \"pushd %L && title Command Prompt\""

[HKEY_CLASSES_ROOT\Drive\shell\cmd]

@="Comm&and Prompt here"

"NoWorkingDirectory"=""

"Extended"=-

[HKEY_CLASSES_ROOT\Drive\shell\cmd\command]

@="cmd.exe /k \"pushd %L && title Command Prompt\""

[HKEY_CLASSES_ROOT\Drive\shell\runas]

@="Ele&vated Comm&and Prompt here"

"NoWorkingDirectory"=""

[HKEY_CLASSES_ROOT\Drive\shell\runas\command]

@="cmd.exe /k \"pushd %L && title Command Prompt\""

That’s it. You can all thank me later. It will also work on XP or 2003, but the Elevation – the four sections above with \runas\ – are typically and I think both would ignore the “NoWorkingDirectory” values. Or you can just download the Command Prompt Here Power Toy. This whole thing was about Vista.

I know: it’s neat – I love it too!


17 thoughts on “Vista Tip: Elevated Command Prompt Here”

  1. Sweet!!! Thanks a lot.

    Question though: Why do you have the ^ and & symbols?…I changed it to “Command Prompt Here” and “Elevated Command Prompt Here”

  2. & is what makes a character an accelerator key – tricky with the Explorer context menu because there are so many letters already taken.

    Didn’t see a ^ though. FWIW, some of the characters are there for the registry file – like the \” and the &&.

  3. No kiddin? Because, that’s exactly what *I* posted *here*.

    However, the post over there is incomplete because it won’t work on the root of a drive, which is why mine works better. And they don’t even mention the Extended part of it… meh. Trolls suck.

  4. Thanks to you and your information about the Extended value, I discovered another hidden context menu entry for copying the full, quoted, path of the selected folder. It’s a feature I use LOTS! Since Vista has it included, I can uninstall the 3rd-party add-on I’ve been using to this point. :-)

    In case anyone missed it, the context menu entry is labelled “Copy as Path” and should be just under “Share…”

  5. Thanks. Firefox displays the “Comm&and” as “Comm^”. IE does it right though. I also realized that .reg file must use ANSI character encoding and not UTF-8. It didn’t work with UTF-8.

  6. That’s a bug. With Firefox I mean… that entire section is contained in a <PRE> tag – that should allow me to & all I want without having to XML-ify it (or put it in a CDATA tag).

    Unless I missed how PRE is supposed to be used these days… but I know in the past PRE was like CODE is on most forums…

  7. @Randy

    This is not a Firefox bug.

    You have to XML-ify content EVEN inside a <PRE> tag.

    <PRE> changes only the meaning of spaces and CR/LF.

  8. I’d argue that… I’m too lazy to check the RFC but I know both Netscape and IE and Safari supported PRE the same way… if Firefox is doing something else with it they’re either in the “minority” as correct or they’re treating it like XHTML’s PRE or something. Either way, I don’t care :)

  9. Here is a version that will run the Visual Studio 2005 batch file beforehand (so that your new command prompt will have csc.exe, msbuild, etc in the PATH).

    Works for VS 2008 as well.

    Windows Registry Editor Version 5.00

    [HKEY_CLASSES_ROOT\Directory\shell\cmd]

    @=”Command Prompt here”

    “NoWorkingDirectory”=””

    “Extended”=-

    [HKEY_CLASSES_ROOT\Directory\shell\cmd\command]

    @=”cmd.exe /k \”pushd %L && \”C:\\Program Files\\Microsoft Visual Studio 8\\VC\\vcvarsall.bat\” x86\””

    [HKEY_CLASSES_ROOT\Directory\shell\runas]

    @=”Elevated Command Prompt here”

    “NoWorkingDirectory”=””

    [HKEY_CLASSES_ROOT\Directory\shell\runas\command]

    @=”cmd.exe /k \”pushd %L && \”C:\\Program Files\\Microsoft Visual Studio 8\\VC\\vcvarsall.bat\” x86\””

    [HKEY_CLASSES_ROOT\Drive\shell\cmd]

    @=”Command Prompt here”

    “NoWorkingDirectory”=””

    “Extended”=-

    [HKEY_CLASSES_ROOT\Drive\shell\cmd\command]

    @=”cmd.exe /k \”pushd %L && \”C:\\Program Files\\Microsoft Visual Studio 8\\VC\\vcvarsall.bat\” x86\””

    [HKEY_CLASSES_ROOT\Drive\shell\runas]

    @=”Elevated Command Prompt here”

    “NoWorkingDirectory”=””

    [HKEY_CLASSES_ROOT\Drive\shell\runas\command]

    @=”cmd.exe /k \”pushd %L && \”C:\\Program Files\\Microsoft Visual Studio 8\\VC\\vcvarsall.bat\” x86\””

  10. Here’s one more you can do: The desktop! I find it useful… and hard to navigate to otherwise.

     

     

    [HKEY_CLASSES_ROOT\Directory\Background\shell\runas\]

    @=”Elevated Command Prompt Here”

    “Extended”=””

    “NoWorkingDirectory”=””

    [HKEY_CLASSES_ROOT\Directory\Background\shell\runas\command]

    @=”cmd.exe /k pushd %V && color 4f”

     

     

    The color is a personal touch, for all my elevated cmd prompts :)

  11. Randy, the Comm^ thing isn’t a Firefox bug, it’s broken HTML. You need to replace the & characters with & – even inside PRE blocks. Get yourself a copy of Firefox and the HTML Validator add-on… I think you’ll be surprised.

    MS-IE only “gets it right” because of all its “quirks fixes” to work around lazy web developers.

  12. Seems stupid of HTML, actually… why have a PRE tag but still require encoding? I guess I could wrap it all in a CDATA tag, but the honest truth is that I don’t post code to the blog often…

  13. Wow – thanks for this. I needed this on Windows Swerver 2008 to delete Exchange LOG files out growing the space daily. What a time saver and it works even on Win 2008 SP2

  14. On Windows 7, you can Shift+Right-Click on a folder, and the menu that comes up has some extra oontpis, including Open Command Window Here and Copy as Path . I believe this feature is enabled by default.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.