How to run command prompt as admin?

English support forum

Moderators: white, Hacker, petermad, Stefan2

avada
Senior Member
Senior Member
Posts: 217
Joined: 2007-10-05, 06:24 UTC

How to run command prompt as admin?

Post by *avada »

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?
User avatar
MVV
Power Member
Power Member
Posts: 8702
Joined: 2008-08-03, 12:51 UTC
Location: Russian Federation

Post by *MVV »

Make a button or a user-command:

Code: Select all

Command: %COMMANDER_PATH%\Sudo.exe
Parameters: cmd.exe /k cd /d %P
You may find Sudo in my signature.
User avatar
HolgerK
Power Member
Power Member
Posts: 5406
Joined: 2006-01-26, 22:15 UTC
Location: Europe, Aachen

Post by *HolgerK »

Or this button:

Code: Select all

TOTALCMD#BAR#DATA
*%COMSPEC%  /k
cd /d %P
%COMSPEC%
Elevated Command Prompt


-1
Regards
Holger
avada
Senior Member
Senior Member
Posts: 217
Joined: 2007-10-05, 06:24 UTC

Post by *avada »

Thanks for the replies
MVV wrote:Make a button or a user-command:

Code: Select all

Command: %COMMANDER_PATH%\Sudo.exe
Parameters: cmd.exe /k cd /d %P
You may find Sudo in my signature.
Thanks. It works quite well without sudo too, with the context menu run as admin option.
One question: What the /d %P do? (The help say it disables somekind of autoron) And how come the path is not after cd?
HolgerK wrote:Or this button:

Code: Select all

TOTALCMD#BAR#DATA
*%COMSPEC%  /k
cd /d %P
%COMSPEC%
Elevated Command Prompt


-1
Regards
Holger
I'm not sure how would I do this? Where would I add all these lines?
User avatar
nsp
Power Member
Power Member
Posts: 1803
Joined: 2005-12-04, 08:39 UTC
Location: Lyon (FRANCE)
Contact:

Post by *nsp »

avada wrote:

Code: Select all

TOTALCMD#BAR#DATA
*%COMSPEC%  /k
cd /d %P
%COMSPEC%
Elevated Command Prompt


-1
I'm not sure how would I do this? Where would I add all these lines?
Select the code in the code part, copy it in the cipboard, right click in TC toolbar and click paste !

for your info, * in front of command replace sudo
User avatar
HolgerK
Power Member
Power Member
Posts: 5406
Joined: 2006-01-26, 22:15 UTC
Location: Europe, Aachen

Post by *HolgerK »

- 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.
What the /d %P do?
cmd.exe: prompt> help cd wrote:Use the /D switch to change current drive in addition to changing current directory for a drive.
Regards
Holger
User avatar
karlchen
Power Member
Power Member
Posts: 4601
Joined: 2003-02-06, 22:23 UTC
Location: Germany

Post by *karlchen »

Hello, avada.
avada 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?
As you use the commandline
cmd.exe /k cd /d %P
inside the parameter field of a Total Commander button the commandline works like this:
  • 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.
Kind regards,
Karl
avada
Senior Member
Senior Member
Posts: 217
Joined: 2007-10-05, 06:24 UTC

Post by *avada »

Thanks for the info.
Windows 10
User avatar
Balderstrom
Power Member
Power Member
Posts: 2148
Joined: 2005-10-11, 10:10 UTC

Post by *Balderstrom »

Interesting, though I don't think you need the "cd /d %P" parameter.

This works just as well:

Code: Select all

TOTALCMD#BAR#DATA
*%COMSPEC%  /k

%COMSPEC%
Elevated Command Prompt


-1
Although, perhaps it's needed for Win7?
User avatar
MVV
Power Member
Power Member
Posts: 8702
Joined: 2008-08-03, 12:51 UTC
Location: Russian Federation

Post by *MVV »

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).
User avatar
gewone
Junior Member
Junior Member
Posts: 66
Joined: 2008-03-06, 17:37 UTC

Re:

Post by *gewone »

Thanks for this guys, found my way here through Google!

However, this still (almost a decade later) seems to be an issue:
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).
I was thinking, has there been any improvement so that there's by any chance perhaps a smoother way nowadays?

Cheers!
User avatar
petermad
Power Member
Power Member
Posts: 14739
Joined: 2003-02-05, 20:24 UTC
Location: Denmark
Contact:

Re: How to run command prompt as admin?

Post by *petermad »

I use this command to start in curren directory

Code: Select all

[em_cmdelevated]
cmd=*%COMSPEC% /C
param=Start /D"%P"
menu=Command Prompt as Administrator
button=cmd.exe
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
License #524 (1994)
Danish Total Commander Translator
TC 11.03 32+64bit on Win XP 32bit & Win 7, 8.1 & 10 (22H2) 64bit, 'Everything' 1.5.0.1371a
TC 3.50b4 on Android 6 & 13
Try: TC Extended Menus | TC Languagebar | TC Dark Help | PHSM-Calendar
avada
Senior Member
Senior Member
Posts: 217
Joined: 2007-10-05, 06:24 UTC

Re: Re:

Post by *avada »

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:
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).
I was thinking, has there been any improvement so that there's by any chance perhaps a smoother way nowadays?

Cheers!
The solution was given way back then, on TC's side of course.
Mine's like this:

Code: Select all

TOTALCMD#BAR#DATA
*%COMSPEC% /u /k
cd /d %P


0
-1
I use it regularly.
Windows 10
User avatar
gewone
Junior Member
Junior Member
Posts: 66
Joined: 2008-03-06, 17:37 UTC

Re: How to run command prompt as admin?

Post by *gewone »

Thanks @avada, amazing!
Exactly what I wanted! 🙏
avada
Senior Member
Senior Member
Posts: 217
Joined: 2007-10-05, 06:24 UTC

Re:

Post by *avada »

HolgerK wrote: 2011-04-07, 13:08 UTC Or this button:

Code: Select all

TOTALCMD#BAR#DATA
*%COMSPEC%  /k
cd /d %P
%COMSPEC%
Elevated Command Prompt


-1
Regards
Holger
I'm wondering how to do this for powershell.
Windows 10
Post Reply