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

magicandre1981
  • 97,301
  • 30
  • 179
  • 245
AngryHacker
  • 18,217
  • 67
  • 156
  • 209

9 Answers9

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.

Bob
  • 60,938
  • 25
  • 191
  • 216
51

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

pacoverflow
  • 1,799
  • 7
  • 33
  • 51
barlop
  • 23,380
  • 43
  • 145
  • 225
  • 9
    Instead of cd, use pushd, then it will also work on other drives than c: and even unc shares. – ths Apr 21 '16 at 06: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:\` – Axel Kemper Apr 21 '16 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 '16 at 13:58
  • cd /d still can't cover unc paths... so a moot point in fact – JasonXA Sep 15 '17 at 02: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 '17 at 04:50
  • @JasonXA furthermore, pushd will create a temporary network drive e.g. as mentioned here https://superuser.com/questions/282963/browse-an-unc-path-using-windows-cmd-without-mapping-it-to-a-network-drive plus you won't see the command in this case the pushd command, that was given.. So the cmd prompt would be there without showing clearly that it's on a network drive and which network drive it is on. the only indication would be a temporary drive letter that looks unfamiliar in terms of what location it refers to... – barlop Sep 15 '17 at 05:10
  • @JasonXA . So if one did use pushd there one might want to change the title of the cmd window too. And really if one was so organised as to create a cmd prompt specifically to access a drive, one would likely be organised enough to have made a permanent drive letter to that drive too, and therefore have no need for pushd. And the permanent drive letter will be far more familiar. pushd on a unc path is great when entered manually as you can see the command was entered not just the output in this case the fairly random drive letter. – barlop Sep 15 '17 at 05:12
  • Yeah, but you advocate bad practices with outdated commands, the reason pushd might be preferred on top of full UNC compatibility is the ability to revert to previous location with popd without having to type the old path again, plus with pushd you don't need to reference network locations by drive. Once a location is set like \\network_host\path you can use the %cd% to get the mounted path. Again, without having to remember or retype full paths. CD was great once but gets outdated, slowly. – JasonXA Sep 17 '17 at 14:41
  • @JasonXA You write "you can use the %cd% to get the mounted path. Again, without having to remember or retype full paths" <-- how is using that CD environment variable, an advantage of using the pushd command over using the cd command? – barlop Sep 17 '17 at 19:57
  • @JasonXA Also, the idea that CD is outdated and pushd and popd are what one should use, is an interesting idea that I haven't heard before,.though the remaining disadvantage of pushd is the extra keystrokes. One could do `doskey cd=pushd $*` and `doskey cdd=popd $*` and then one would have an updated 'cd'. I still don't think pushd is an advantage in this case, and if anything using it on a UNC path here , when starting a cmd prompt, is a disadavantage as it gives some temporary drive letter that won't make it immediately clear what actual drive is being referenced. – barlop Sep 17 '17 at 20:07
  • Is there any way i can open this in any folder? When i click `Shift + Right Click`, i get _Open command windows here_. Can this somehow be customized to open the Administrator windows in whatever current folder i am in? I am using Windows Server 2016. – Technext Aug 28 '18 at 07:14
  • @Technext you could look into what can be done with customizing the context menu, there may be some native or third party way to do that. – barlop May 15 '20 at 18:10
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).

magicandre1981
  • 97,301
  • 30
  • 179
  • 245
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").

Ben N
  • 40,045
  • 17
  • 140
  • 181
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.

DavidPostill
  • 153,128
  • 77
  • 353
  • 394
Zaibis
  • 143
  • 10
  • This was the first thought I had as well, but it isn't the question. – Raystafarian Apr 21 '16 at 15:18
  • if we had a way to do this elevated, i'd be happy. – ths Apr 21 '16 at 15:34
  • 1
    @ths You can [add](http://michaelcrump.net/power-tip-elevated-command-prompt-here-in-windows-8-1/) the option. – Bob Apr 21 '16 at 23:44
  • 1
    thanks @Bob , with an "Extended" key, it will even be in the shift-click menu, too. – ths Apr 22 '16 at 08: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 '16 at 09:06
  • The more elegant solution is this one, plus adding whatever he had in that path to the SYSTEM PATH making them available from any project folder. Sometimes people overcomplicate things by simple means. You could also add and empty REG_SZ named HasLUAShield under runas which will show that is requires admin privileges. – JasonXA Sep 15 '17 at 02:47
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)
Broper
  • 111
  • 1
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.

youngrrrr
  • 111
  • 3
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
phuclv
  • 26,555
  • 15
  • 113
  • 235
-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"

LordKiz
  • 117
  • 2
  • 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? – Vomit IT - Chunky Mess Style Jul 09 '17 at 16:12