How to open Command Prompt in a specific folder as Administrator?
Solution 1
In the target you should specify cmd /k cd c:\crp
Solution 2
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.
Solution 3
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.
Now you can click on this icon in every folder you like and the cmd now opens in this folder.
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.
If you now press the number the tool at this position is started (in my case 4 runs the cmd as admin).
Solution 4
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").
Solution 5
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.
Related videos on Youtube
gardenofwine
Updated on September 18, 2022Comments
-
gardenofwine over 1 year
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?
-
ths about 8 yearsInstead of cd, use pushd, then it will also work on other drives than c: and even unc shares.
-
Martin Braun about 8 years+1 for explaining the reason, I always thought it's a bug, lol.
-
Axel Kemper about 8 yearsTo 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:`
-
barlop about 8 years@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).
-
Raystafarian about 8 yearsThis was the first thought I had as well, but it isn't the question.
-
ths about 8 yearsif we had a way to do this elevated, i'd be happy.
-
tvdo about 8 years@ths You can add the option.
-
ths about 8 yearsthanks @Bob , with an "Extended" key, it will even be in the shift-click menu, too.
-
Zaibis about 8 years@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.
-
doctorrickb about 8 yearsCan this be turned into a shortcut?
-
magicandre1981 about 8 years@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
-
Aaron over 7 yearsthis is perfect solution :)
-
Vomit IT - Chunky Mess Style almost 7 yearsCome 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?
-
JasonXA over 6 yearsThe 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 over 6 yearscd /d still can't cover unc paths... so a moot point in fact
-
barlop over 6 years@JasonXA that has already been said in comment in the comment right above yours, as well as in the first comment.
-
barlop over 6 years@JasonXA furthermore, pushd will create a temporary network drive e.g. as mentioned here superuser.com/questions/282963/… 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 over 6 years@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.
-
JasonXA over 6 yearsYeah, 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.
-
barlop over 6 years@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 over 6 years@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 $*
anddoskey 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. -
Technext over 5 yearsIs 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. -
Paramvir Singh Karwal over 4 yearsCrazy hack! On windows 10 you could use
powershell
instead ofcmd
option. -
magicandre1981 over 4 years@ParamvirSinghKarwal yes, in Win10, Microsoft removed cmd open and replaced the cmd entry with powershell since Update Version 1703
-
Rich Lysakowski PhD over 4 yearsExcellent explanation of a rather obscure mechanism.
-
barlop about 4 years@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.