How to open Command Prompt in a specific folder as Administrator?

41

12

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

AngryHacker

Posted 2016-04-20T22:38:26.857

Reputation: 14 731

Answers

42

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

barlop

Posted 2016-04-20T22:38:26.857

Reputation: 18 677

7Instead of cd, use pushd, then it will also work on other drives than c: and even unc shares. – ths – 2016-04-21T06:39:01.183

2To 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 – 2016-04-21T13:56:39.160

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 – 2016-04-21T13:58:37.377

cd /d still can't cover unc paths... so a moot point in fact – JasonXA – 2017-09-15T02:53:32.087

@JasonXA that has already been said in comment in the comment right above yours, as well as in the first comment. – barlop – 2017-09-15T04:50:06.377

@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 – 2017-09-15T05:10:33.150

@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 – 2017-09-15T05:12:54.773

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 – 2017-09-17T14:41:21.217

@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 – 2017-09-17T19:57:45.273

@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 – 2017-09-17T20:07:16.243

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 – 2018-08-28T07:14:13.213

50

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

Posted 2016-04-20T22:38:26.857

Reputation: 51 526

4+1 for explaining the reason, I always thought it's a bug, lol. – Martin Braun – 2016-04-21T10:13:13.817

Excellent explanation of a rather obscure mechanism. – Rich Lysakowski PhD – 2019-12-15T04:38:44.943

28

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

Posted 2016-04-20T22:38:26.857

Reputation: 86 560

Can this be turned into a shortcut? – None – 2016-04-22T11:29:38.777

2@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 – magicandre1981 – 2016-04-22T14:56:03.580

3this is perfect solution :) – Aaron – 2016-09-14T09:00:32.547

Crazy hack! On windows 10 you could use powershell instead of cmd option. – Paramvir Singh Karwal – 2019-09-19T12:23:49.750

@ParamvirSinghKarwal yes, in Win10, Microsoft removed cmd open and replaced the cmd entry with powershell since Update Version 1703 – magicandre1981 – 2019-09-19T14:17:58.287

11

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

Posted 2016-04-20T22:38:26.857

Reputation: 32 973

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.

Zaibis

Posted 2016-04-20T22:38:26.857

Reputation: 143

This was the first thought I had as well, but it isn't the question. – Raystafarian – 2016-04-21T15:18:09.267

if we had a way to do this elevated, i'd be happy. – ths – 2016-04-21T15:34:59.360

1

@ths You can add the option.

– Bob – 2016-04-21T23:44:07.787

1thanks @Bob , with an "Extended" key, it will even be in the shift-click menu, too. – ths – 2016-04-22T08:42:15.653

@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 – 2016-04-22T09:06:17.977

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 – 2017-09-15T02:47:58.740

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

Posted 2016-04-20T22:38:26.857

Reputation: 111

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

Posted 2016-04-20T22:38:26.857

Reputation: 111

0

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

Posted 2016-04-20T22:38:26.857

Reputation: 119

2Come 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? – Pimp Juice IT – 2017-07-09T16:12:32.057