Skip to content

Instantly share code, notes, and snippets.

@jordanbtucker
Last active April 16, 2024 18:51
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save jordanbtucker/3b2585394fb127719a78 to your computer and use it in GitHub Desktop.
Save jordanbtucker/3b2585394fb127719a78 to your computer and use it in GitHub Desktop.
Add an "Open command window here (Admin)" context menu to folders
@echo off
net session > nul 2>&1
if /i not %errorlevel%==0 (
echo You must run this from an elevated prompt.
goto:eof
)
call :setreg "HKCR\Directory\shell\runas"
call :setreg "HKCR\Directory\Background\shell\runas"
:setreg
call :setrunas "%~1"
call :setcommand "%~1\command"
goto :eof
:setrunas
call :setregval "%~1" /d "Open command window here (&Admin)"
call :setregval "%~1" /v Extended
call :setregval "%~1" /v NoWorkingDirectory
goto :eof
:setcommand
call :setregval "%~1" /d "cmd.exe /s /k pushd \"%%%%V\""
goto :eof
:setregval
reg add "%~1" "%~2" "%~3" /f > nul 2>&1
goto :eof
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\shell\runas]
@="Open command window here (&Admin)"
"Extended"=""
"NoWorkingDirectory"=""
[HKEY_CLASSES_ROOT\Directory\shell\runas\command]
@="cmd.exe /s /k pushd \"%V\""
[HKEY_CLASSES_ROOT\Directory\Background\shell\runas]
@="Open command window here (&Admin)"
"Extended"=""
"NoWorkingDirectory"=""
[HKEY_CLASSES_ROOT\Directory\Background\shell\runas\command]
@="cmd.exe /s /k pushd \"%V\""

Open command window here (Admin)

Shift+right-clicking a folder, or its background, will give you extra commands like Open command window here and Copy as path. This Registry file will add an option for Open command window here (Admin), which as implied will open a Command Prompt as Administrator.

Ether run the batch file in an elevated prompt or run the reg file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment