How to get in current directory as Admin?

English support forum

Moderators: white, Hacker, petermad, Stefan2

Post Reply
User avatar
vdijken
Member
Member
Posts: 181
Joined: 2016-07-30, 14:07 UTC
Location: The Netherlands

How to get in current directory as Admin?

Post by *vdijken »

I use TC 64 bit on the latest Windows version 21H1. I made a button with cmd.exe as executable and . as working directory. This works fine, but when I execute the button as Administrator I land up in c:\windows\system32. I tried to find an answer in previous topics, but I had no luck. So how can I smooth this out so that I always end up in the actual directory?
User avatar
white
Power Member
Power Member
Posts: 4620
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: How to get in current directory as Admin?

Post by *white »

Code: Select all

TOTALCMD#BAR#DATA
cmd.exe /k
%B+ && cd %P
C:\Windows\System32\cmd.exe
cmd


-1
Paste above on button bar.
User avatar
Stefan2
Power Member
Power Member
Posts: 4157
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Re: How to get in current directory as Admin?

Post by *Stefan2 »

vdijken wrote: 2022-03-11, 16:46 UTC I use TC 64 bit on the latest Windows version 21H1.
I made a button with cmd.exe as executable and . as working directory.
This works fine, but when I execute the button as Administrator I land up in c:\windows\system32.
That the current path is dropped if launched CMD as Admin is a know windows bug.


This command should work too:

CMD: *%SystemRoot%\system32\cmd.exe
Para: /K CD /D "%P"
Path: <leave empty for current path from active panel , no need to use an dot here>
Icon: cmd.exe
Tooltip: run CMD with admin rights





 
User avatar
Horst.Epp
Power Member
Power Member
Posts: 6489
Joined: 2003-02-06, 17:36 UTC
Location: Germany

Re: How to get in current directory as Admin?

Post by *Horst.Epp »

I find White's version better.
It allows to use the cmd button as normal user and with the context menu "As Administrator"
Windows 11 Home x64 Version 23H2 (OS Build 22631.3447)
TC 11.03 x64 / x86
Everything 1.5.0.1372a (x64), Everything Toolbar 1.3.3, Listary Pro 6.3.0.73
QAP 11.6.3.2 x64
User avatar
nsp
Power Member
Power Member
Posts: 1805
Joined: 2005-12-04, 08:39 UTC
Location: Lyon (FRANCE)
Contact:

Re: How to get in current directory as Admin?

Post by *nsp »

If you use network share or subst this will not work !
Cmd do not support virtual folder like \\Documents, \\Downloads \\Quick Access ... even with normal user !


In the case of share/subst, I advise to use a batch which use/mount network share, or create subst one by one if folder is not found and then do the CD.
or a kind of automount batch you call once (the credentials and used share are stored system wise but the usage is by user) and then Change Directory to the pointed folder.


----- Edited (Alternate solution) for normal folder and shares
You can use the gsudo which do the CD and the nount for you and cache UAC if configured. It will not ask you each time UAC dialog and you can configure prompt ....
For shared folder, you must call it once from a non shared folder and then the mount will stay and it will prevent a bug when calling gsudo from non known folder....

Code: Select all

TOTALCMD#BAR#DATA
TOTALCMD#BAR#DATA
gsudo
--copyns
C:\Windows\System32\cmd.exe
cmd as Admin (gsudo)


-1
User avatar
Horst.Epp
Power Member
Power Member
Posts: 6489
Joined: 2003-02-06, 17:36 UTC
Location: Germany

Re: How to get in current directory as Admin?

Post by *Horst.Epp »

There is a registry key which allows cmd on UNC pathes.
I applied it years ago (I have to search for it).
Windows 11 Home x64 Version 23H2 (OS Build 22631.3447)
TC 11.03 x64 / x86
Everything 1.5.0.1372a (x64), Everything Toolbar 1.3.3, Listary Pro 6.3.0.73
QAP 11.6.3.2 x64
User avatar
white
Power Member
Power Member
Posts: 4620
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: How to get in current directory as Admin?

Post by *white »

white wrote: 2022-03-11, 17:01 UTC

Code: Select all

TOTALCMD#BAR#DATA
cmd.exe /k
%B+ && cd %P
C:\Windows\System32\cmd.exe
cmd


-1
Paste above on button bar.
Stefan2 wrote: 2022-03-11, 18:00 UTC CMD: *%SystemRoot%\system32\cmd.exe
Para: /K CD /D "%P"
Path: <leave empty for current path from active panel , no need to use an dot here>
Icon: cmd.exe
Tooltip: run CMD with admin rights
Horst.Epp wrote: 2022-03-11, 18:30 UTC I find White's version better.
It allows to use the cmd button as normal user and with the context menu "As Administrator"
In that case you can use Stefan's solution and simply remove the * and adjust the tooltip.

Using full path to cmd.exe is better, because otherwise it would launch for example cmd.exe in the currently displayed folder if there is one.
Not sure why he uses %SystemRoot%\system32\cmd.exe instead of %comspec%. Perhaps to prevent execution of an incompatible command processor. I think I prefer %comspec%.

Putting /k in the parameters field is better because otherwise if the user would drag and drop file on the button, cmd.exe would be started and the file would be started as well.

Using cd with parameter /d is better as well, because it uses 1 command instead of 2.

Adding quotes around %P makes sure it also works in the rare case where Command Extensions are disabled by default by a registry setting.

Code: Select all

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\EnableExtensions
HKEY_CURRENT_USER\Software\Microsoft\Command Processor\EnableExtensions
Or when Command Extensions are disabled by a setlocal command started due to Autorun registry settings. See below.
nsp wrote: 2022-03-12, 07:14 UTC Cmd do not support virtual folder like \\Documents, \\Downloads \\Quick Access ... even with normal user !
Good point. We could catch the error and exit the command prompt in that case by added "||exit" to the parameters.
Or pause first so the user can read the error message.
Or display a fancy error message.

Code: Select all

Parameters:	/k cd /d "%P" || exit
Parameters:	/k cd /d "%P" || (pause & exit)
Parameters:	/k cd /d "%P" 2>nul || (echo Cannot go to the current (virtual^) folder in Command Prompt! & pause & exit)
So, I would use:

Code: Select all

Command:	%comspec%
Parameters:	/k cd /d "%P" || exit
Start path:	
Icon file:	%comspec%
Tooltip:	Command Prompt


nsp wrote: 2022-03-12, 07:14 UTC If you use network share or subst this will not work !
...
In the case of share/subst, I advise to use a batch which use/mount network share, or create subst one by one if folder is not found and then do the CD.
or a kind of automount batch you call once (the credentials and used share are stored system wise but the usage is by user) and then Change Directory to the pointed folder.
If you want your subst drive or network mount to be available for user Administrator as well, you could set that up for that user.
If you want your subst drive or network mount to be available globally for the machine, you could set that up.
If you want your subst drive or network mount to be available for CMD specificly, you can use:

Code: Select all

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun
HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun
Horst.Epp wrote: 2022-03-12, 08:40 UTC There is a registry key which allows cmd on UNC pathes.
I applied it years ago (I have to search for it).

Code: Select all

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor\DisableUNCCheck

with value:
0x00000001 (1)
Or use the pushd command instead of the cd command.
User avatar
petermad
Power Member
Power Member
Posts: 14807
Joined: 2003-02-05, 20:24 UTC
Location: Denmark
Contact:

Re: How to get in current directory as Admin?

Post by *petermad »

I use this - it is a little simpler:

Code: Select all

TOTALCMD#BAR#DATA
%COMSPEC% /C
Start /D"%P"
%COMSPEC%
Command Prompt in current directory - also  as Administrator


-1
Last edited by petermad on 2022-03-12, 17:17 UTC, edited 2 times in total.
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.50 on Android 6 & 13
Try: TC Extended Menus | TC Languagebar | TC Dark Help | PHSM-Calendar
User avatar
white
Power Member
Power Member
Posts: 4620
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: How to get in current directory as Admin?

Post by *white »

white wrote: 2022-03-12, 11:05 UTC Putting /k in the parameters field is better because otherwise if the user would drag and drop file on the button, cmd.exe would be started and the file would be started as well.
Come to think of it, it's even better to put "/k cd /d" in the command field. That way you can drag and drop a folder on the button as well which is kinda neat :)

Code: Select all

Command:	%comspec% /k cd /d
Parameters:	"%P" || exit
Start path:	
Icon file:	%comspec%
Tooltip:	Command Prompt
Or using petermad's method:

Code: Select all

Command:	%comspec% /c start /d
Parameters:	"%P"
Start path:	
Icon file:	%comspec%
Tooltip:	Command Prompt
2petermad
Why does it say "as Administrator" in your tooltip?
User avatar
petermad
Power Member
Power Member
Posts: 14807
Joined: 2003-02-05, 20:24 UTC
Location: Denmark
Contact:

Re: How to get in current directory as Admin?

Post by *petermad »

2petermad
Why does it say "as Administrator" in your tooltip?
Oh , sorry I copied a button where I have * in front of %COMSPEC% - I have corrected my previous post now.
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.50 on Android 6 & 13
Try: TC Extended Menus | TC Languagebar | TC Dark Help | PHSM-Calendar
Post Reply