How to get in current directory as Admin?
Moderators: Hacker, petermad, Stefan2, white
How to get in current directory as Admin?
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?
Re: How to get in current directory as Admin?
Code: Select all
TOTALCMD#BAR#DATA
cmd.exe /k
%B+ && cd %P
C:\Windows\System32\cmd.exe
cmd
-1
Re: How to get in current directory as Admin?
That the current path is dropped if launched CMD as Admin is a know windows bug.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.
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
Re: How to get in current directory as Admin?
I find White's version better.
It allows to use the cmd button as normal user and with the context menu "As Administrator"
It allows to use the cmd button as normal user and with the context menu "As Administrator"
Windows 11 Home, Version 24H2 (OS Build 26100.4061)
TC 11.55 RC2 x64 / x86
Everything 1.5.0.1391a (x64), Everything Toolbar 1.5.2.0, Listary Pro 6.3.2.88
QAP 11.6.4.4 x64
TC 11.55 RC2 x64 / x86
Everything 1.5.0.1391a (x64), Everything Toolbar 1.5.2.0, Listary Pro 6.3.2.88
QAP 11.6.4.4 x64
Re: How to get in current directory as Admin?
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....
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
Re: How to get in current directory as Admin?
There is a registry key which allows cmd on UNC pathes.
I applied it years ago (I have to search for it).
I applied it years ago (I have to search for it).
Windows 11 Home, Version 24H2 (OS Build 26100.4061)
TC 11.55 RC2 x64 / x86
Everything 1.5.0.1391a (x64), Everything Toolbar 1.5.2.0, Listary Pro 6.3.2.88
QAP 11.6.4.4 x64
TC 11.55 RC2 x64 / x86
Everything 1.5.0.1391a (x64), Everything Toolbar 1.5.2.0, Listary Pro 6.3.2.88
QAP 11.6.4.4 x64
Re: How to get in current directory as Admin?
white wrote: 2022-03-11, 17:01 UTCPaste above on button bar.Code: Select all
TOTALCMD#BAR#DATA cmd.exe /k %B+ && cd %P C:\Windows\System32\cmd.exe cmd -1
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
In that case you can use Stefan's solution and simply remove the * and adjust the tooltip.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"
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
Good point. We could catch the error and exit the command prompt in that case by added "||exit" to the parameters.nsp wrote: 2022-03-12, 07:14 UTC Cmd do not support virtual folder like \\Documents, \\Downloads \\Quick Access ... even with normal user !
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)
Code: Select all
Command: %comspec%
Parameters: /k cd /d "%P" || exit
Start path:
Icon file: %comspec%
Tooltip: Command Prompt
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.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 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)
Re: How to get in current directory as Admin?
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.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: How to get in current directory as Admin?
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 neatwhite 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.

Code: Select all
Command: %comspec% /k cd /d
Parameters: "%P" || exit
Start path:
Icon file: %comspec%
Tooltip: Command Prompt
Code: Select all
Command: %comspec% /c start /d
Parameters: "%P"
Start path:
Icon file: %comspec%
Tooltip: Command Prompt
Why does it say "as Administrator" in your tooltip?
Re: How to get in current directory as Admin?
Oh , sorry I copied a button where I have * in front of %COMSPEC% - I have corrected my previous post now.2petermad
Why does it say "as Administrator" in your tooltip?
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