49

I made a shortcut to cmd.exe and specified the folder that I want to start in. I then went to Advanced and ticked Run as administrator.

When I double-click on the shortcut, it always starts in C:\Windows\System32.

What I am missing? How can I get the command line to start in the folder specified?

shortcut properties

the prompt, in System32

0

9 Answers 9

51

In the target you should specify cmd /k cd c:\crp

12
  • 9
    Instead of cd, use pushd, then it will also work on other drives than c: and even unc shares.
    – ths
    Apr 21, 2016 at 6:39
  • 3
    To make the admin status more visible, window title and window color can be set. Example: `cmd.exe /K title Elevated && color 1a && cd /D c:` Apr 21, 2016 at 13:56
  • 2
    @ths pushd works for unc where cd doesn't, indeed. But your slight suggestion that cd won't change to other drives is incorrect, see cd /d (as you probably know).
    – barlop
    Apr 21, 2016 at 13:58
  • cd /d still can't cover unc paths... so a moot point in fact
    – JasonXA
    Sep 15, 2017 at 2:53
  • @JasonXA that has already been said in comment in the comment right above yours, as well as in the first comment.
    – barlop
    Sep 15, 2017 at 4:50
58

If you want the reasoning behind it, the Start in is explicitly ignored when elevation is performed (only on binaries that are part of Windows itself) to protect against a potential security vulnerability.

The basic idea is that potentially-malicious DLLs located in the working directory might be controlled by a user other than the current admin, and can then be loaded with high privileges. To prevent this, UAC will reset the working directory. Because shortcuts' "Start in" is set before elevation occurs, this gets reset during elevation. In an ideal world, this protection would apply to all elevations, but it only applies to built-in Windows binaries because it breaks some third-party programs that expect the working directory to be preserved.

The other answers bypass this by telling the elevated cmd to change its working directory after elevation occurs, via the /k argument.

2
  • 5
    +1 for explaining the reason, I always thought it's a bug, lol. Apr 21, 2016 at 10:13
  • 1
    Excellent explanation of a rather obscure mechanism. Dec 15, 2019 at 4:38
32

Inside Explorer, there is a Quick Access Toolbar (QAT) since the intruduction of the Ribbon in Windows 8. Click on File->open CMD prompt->Open CMD prompt as admin and make a rightclick and select to pin it to QAT.

enter image description here

Now you can click on this icon in every folder you like and the cmd now opens in this folder.

enter image description here

To run the cmd as admin faster from the QAT, press the ALT key and you see a number for the position in the QAT.

enter image description here

If you now press the number the tool at this position is started (in my case 4 runs the cmd as admin).

5
  • Can this be turned into a shortcut?
    – user366447
    Apr 22, 2016 at 11:29
  • 4
    @Mast, press the ALT key and you see a number for the position in the QAT. If you now press the number the tool at this position is started Apr 22, 2016 at 14:56
  • 3
    this is perfect solution :)
    – A.T.
    Sep 14, 2016 at 9:00
  • Crazy hack! On windows 10 you could use powershell instead of cmd option. Sep 19, 2019 at 12:23
  • @ParamvirSinghKarwal yes, in Win10, Microsoft removed cmd open and replaced the cmd entry with powershell since Update Version 1703 Sep 19, 2019 at 14:17
12

Barlop is correct. If you add /k cd "\path\to\folder" to the Target field (after cmd.exe), the resultant Command Prompt window will execute the cd command and then leave you with a prompt to do with as you please. If you need to change to a different drive, you'll need cd /d rather than just cd. If you need to change to a network drive, use pushd instead - it automatically mounts the target UNC path as a drive and changes to it.

The /k switch to cmd means "do this command and keep the prompt open." Everything after the /k is treated as a literal command, so you don't have to worry about escaping. You can use && to execute multiple commands: cd "\path\to\folder" && echo Hi! will produce a prompt in that directory with Hi! printed at the top.

The equivalent of /k that doesn't keep the prompt around is /c (for "execute this command").

3

This is actually not what you are asking for, but when I understand you right this will achieve what you try to work around. try to shift + rightclick on the specific folder and select open command window here. In case thats not your problem, just let me know and I'll remove this.

And (thanks to Bob; Didn't know this before aswell) it is even possible to do this elevated.

Source from Bob's comment.

[...]While we are here we can also add the required keys to open an elevated prompt of every drive letter attached to the system. This can easily be accomplished by adding an entry in the registry. So copy/paste the text below into a text file and give it a name of something like “admin.reg” and double-click it.

Windows Registry Editor Version 5.00    
[-HKEY_CLASSES_ROOT\Directory\shell\runas]       
[HKEY_CLASSES_ROOT\Directory\shell\runas]  @="Open command window here as Administrator"  "HasLUAShield"=""    
[HKEY_CLASSES_ROOT\Directory\shell\runas\command]  @="cmd.exe /s /k pushd \"%V\""    
[-HKEY_CLASSES_ROOT\Directory\Background\shell\runas]    
[HKEY_CLASSES_ROOT\Directory\Background\shell\runas]  @="Open command window here as Administrator"  "HasLUAShield"="" 
[HKEY_CLASSES_ROOT\Directory\Background\shell\runas\command]  @="cmd.exe /s /k pushd \"%V\""    
[-HKEY_CLASSES_ROOT\Drive\shell\runas]    
[HKEY_CLASSES_ROOT\Drive\shell\runas]  @="Open command window here as Administrator"  "HasLUAShield"=""    
[HKEY_CLASSES_ROOT\Drive\shell\runas\command]  @="cmd.exe /s /k pushd \"%V\""

Now if you shift-right click on any folder you will see both options.

6
  • This was the first thought I had as well, but it isn't the question. Apr 21, 2016 at 15:18
  • if we had a way to do this elevated, i'd be happy.
    – ths
    Apr 21, 2016 at 15:34
  • 1
    @ths You can add the option.
    – Bob
    Apr 21, 2016 at 23:44
  • 1
    thanks @Bob , with an "Extended" key, it will even be in the shift-click menu, too.
    – ths
    Apr 22, 2016 at 8:42
  • @Raystafarian: I know, and I feel kinda unpleasant by answering it while beeing aware of it. I could understand people downvoting it because of that. But on the other hand, I could imagine quite a lot of people finding this OP by websearch could be even more comfortable with this solution for their needs. Thats also why I did not just leave this as comment to OP, as that one later just might get removed.
    – Zaibis
    Apr 22, 2016 at 9:06
1

For anyone, like myself, who stumbled upon this when trying to open cmd as admin in a separate drive ("E:" in my case), you may have experienced problems when trying to cd E:/somedir or the like. Doing this will not actually change directory to the new drive.

If this is your problem and you just need to use cmd as admin on a different drive, the cmd syntax is actually just E: (or whatever drive letter), with no cd command.

So you can just:

  1. Windows Search > "cmd" > Right click > Run as Administrator
  2. Simply type E: (or whatever drive letter you wish to switch to followed by a colon)
1

If you need to start up from a different drive (e.g. D:\something) than your system drive (i.e. C:\Windows\system32), you can put the following to your "Target" field:

cmd /k "cd /d D:\<your directory here>"

The key here is adding the /d option in front of your cd.

0

Follow below keyboard shortcuts

  • Go to the specific folder by opening File Explorer -> Press Windows+E
  • To Open Windows Powershell command window in Admin mode (with current path) -> Press Alt+F+S+A
-1

When you open command prompt as administrator and it is showing you "C:\Windows\System32" all you have to do is to enter "cd .." and it will take you up one directory which will be "C:\Windows". Repeat "cd .." again to go to "C:\" from where you can now "cd Users/name/folder"

1
  • 3
    Come on for real.... If you want to put an answer like that, then learn how to add a lot more detail and be more clear. If you have a quicker way or doing this than what has already been specified in other answers, then why don't you show it with some more clear detail. I can think of a way but I'm not gonna do your work for you but could easily make a legit answer out of this and make it actually acceptable... easily... just type, read, test, learn, etc. Come on for real Kligmaker you cannot do better than that with your idea? Jul 9, 2017 at 16:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .