How to run command prompt as admin?
Moderators: Hacker, petermad, Stefan2, white
How to run command prompt as admin?
Hi!
I often need to run an elevated command promt for some command line programs.
Within TC I can only do that conveniently with a button bar button. Can't use cm_ExecuteDOS because then the run as admin option does not appear in the context menu. So I'm left with the "cmd" command. The problem is that when I run as admin the working directry changes to C:\Windows\system32\ unlike when I run it normally, in which case the working directory will be the current directory in TC. Needing to navigate from system32 to wherever I have the executable adds a lot of tediousness.
I tried everything I could think of in the parameter and working folder fileds of the button, but nothing worked. How can I start the command prompt with admin privileges and keeping the current directory as the working directory?
I often need to run an elevated command promt for some command line programs.
Within TC I can only do that conveniently with a button bar button. Can't use cm_ExecuteDOS because then the run as admin option does not appear in the context menu. So I'm left with the "cmd" command. The problem is that when I run as admin the working directry changes to C:\Windows\system32\ unlike when I run it normally, in which case the working directory will be the current directory in TC. Needing to navigate from system32 to wherever I have the executable adds a lot of tediousness.
I tried everything I could think of in the parameter and working folder fileds of the button, but nothing worked. How can I start the command prompt with admin privileges and keeping the current directory as the working directory?
Make a button or a user-command:
You may find Sudo in my signature.
Code: Select all
Command: %COMMANDER_PATH%\Sudo.exe
Parameters: cmd.exe /k cd /d %P
Or this button:
Regards
Holger
Code: Select all
TOTALCMD#BAR#DATA
*%COMSPEC% /k
cd /d %P
%COMSPEC%
Elevated Command Prompt
-1
Holger
Thanks for the replies
One question: What the /d %P do? (The help say it disables somekind of autoron) And how come the path is not after cd?
Thanks. It works quite well without sudo too, with the context menu run as admin option.MVV wrote:Make a button or a user-command:
You may find Sudo in my signature.Code: Select all
Command: %COMMANDER_PATH%\Sudo.exe Parameters: cmd.exe /k cd /d %P
One question: What the /d %P do? (The help say it disables somekind of autoron) And how come the path is not after cd?
I'm not sure how would I do this? Where would I add all these lines?HolgerK wrote:Or this button:RegardsCode: Select all
TOTALCMD#BAR#DATA *%COMSPEC% /k cd /d %P %COMSPEC% Elevated Command Prompt -1
Holger
Select the code in the code part, copy it in the cipboard, right click in TC toolbar and click paste !avada wrote:I'm not sure how would I do this? Where would I add all these lines?Code: Select all
TOTALCMD#BAR#DATA *%COMSPEC% /k cd /d %P %COMSPEC% Elevated Command Prompt -1
for your info, * in front of command replace sudo
- mark the text inside oft the code block, Copy to clipboard
- Context menu oft the button bar -> "Paste"
the * (>=TC7.56) in front of the %comspec% does a very similar job like sudo.exe: The button will always start the program elevated, no need for context menu.
Holger
- Context menu oft the button bar -> "Paste"
the * (>=TC7.56) in front of the %comspec% does a very similar job like sudo.exe: The button will always start the program elevated, no need for context menu.
What the /d %P do?
Regardscmd.exe: prompt> help cd wrote:Use the /D switch to change current drive in addition to changing current directory for a drive.
Holger
Hello, avada.
Karl
As you use the commandlineavada wrote:[...]Code: Select all
Command: %COMMANDER_PATH%\Sudo.exe Parameters: cmd.exe /k cd /d %P
One question: What the /d %P do? (The help say it disables somekind of autoron) And how come the path is not after cd?
inside the parameter field of a Total Commander button the commandline works like this:cmd.exe /k cd /d %P
- T.C. replaces %P by the current source panel foldername. - So there is a path behind the cd command!
- cmd.exe /k opens a command prompt window which will close once you type exit or click the X in upper right hand corner of the window.
cd /d %P tells cmd.exe to change to the folder given in the variable %P.
As explained above, T.C. will already have replaced %P by the full pathname before launching cmd.exe.
/d makes sure that the cd command changes to the appropriate drive if neccessary.
Karl
- Balderstrom
- Power Member
- Posts: 2148
- Joined: 2005-10-11, 10:10 UTC
Interesting, though I don't think you need the "cd /d %P" parameter.
This works just as well:
Although, perhaps it's needed for Win7?
This works just as well:
Code: Select all
TOTALCMD#BAR#DATA
*%COMSPEC% /k
%COMSPEC%
Elevated Command Prompt
-1
Re:
Thanks for this guys, found my way here through Google!
However, this still (almost a decade later) seems to be an issue:
Cheers!
However, this still (almost a decade later) seems to be an issue:
I was thinking, has there been any improvement so that there's by any chance perhaps a smoother way nowadays?MVV wrote: 2011-04-08, 06:29 UTC Balderstrom, stupid Windows sets current directory to C:\Windows\System32 when you start elevated application or batch file (it's a real pain when you need to do something in current directory).
Cheers!
Re: How to run command prompt as admin?
I use this command to start in curren directory
In contrary to previous suggestions I add quotation marks around %P - because TC does not set them automatically unless %P is used together with %n or %S
Code: Select all
[em_cmdelevated]
cmd=*%COMSPEC% /C
param=Start /D"%P"
menu=Command Prompt as Administrator
button=cmd.exe
License #524 (1994)
Danish Total Commander Translator
TC 11.51 32+64bit on Win XP 32bit & Win 7, 8.1 & 10 (22H2) 64bit, 'Everything' 1.5.0.1391a
TC 3.60b4 on Android 6, 13, 14
TC Extended Menus | TC Languagebar | TC Dark Help | PHSM-Calendar
Danish Total Commander Translator
TC 11.51 32+64bit on Win XP 32bit & Win 7, 8.1 & 10 (22H2) 64bit, 'Everything' 1.5.0.1391a
TC 3.60b4 on Android 6, 13, 14
TC Extended Menus | TC Languagebar | TC Dark Help | PHSM-Calendar
Re: Re:
The solution was given way back then, on TC's side of course.gewone wrote: 2021-03-27, 21:07 UTC Thanks for this guys, found my way here through Google!
However, this still (almost a decade later) seems to be an issue:
I was thinking, has there been any improvement so that there's by any chance perhaps a smoother way nowadays?MVV wrote: 2011-04-08, 06:29 UTC Balderstrom, stupid Windows sets current directory to C:\Windows\System32 when you start elevated application or batch file (it's a real pain when you need to do something in current directory).
Cheers!
Mine's like this:
Code: Select all
TOTALCMD#BAR#DATA
*%COMSPEC% /u /k
cd /d %P
0
-1
Windows 10
Re: How to run command prompt as admin?
Thanks @avada, amazing!
Exactly what I wanted!
Exactly what I wanted!