How to create a "run as admin" bash shortcut
To run bash console (Windows) as admin in a project folder, currently I first launch bash as admin then manually navigate to the folder which is excruciatingly laborious. Any way to get a graphical shortcut directly into this folder for "run as admin" bash console?
Windows Subsystem for Linux (WSL) can be launched through the bash
command (usually located at C:\Windows\System32\bash.exe
) from any Windows shell. This command drops you into a Linux bash
shell running in the current directory, which means this is actually really easy to do.
(Of course, having said that, the command turns out to be surprisingly ugly.)
The command you want, in general, is this: powershell -c start -verb runas cmd '/c start /D "<dirname>" bash.exe'
We use Powershell's start
command, which supports launching a program as admin via -verb runas
. However, if the program being run is in System32, then its working directory will always start as System32. Thus, have cmd
(or powershell
) use their start
command again to launch bash
with the desired working directory. How we set that directory varies, though.
Note that all of these methods will cause the UAC prompt to appear to be for Powershell.
Using the Windows Explorer folder context menu
This lets you right-click on any folder to launch bash as Admin there.
If you want screenshots and such, see here:
- Open
regedit
and navigate to HKEY_CLASSES_ROOT\Directory\shell - Add a new subkey, call it something like "AdminBash"
- Change the default (string) value of the new subkey to whatever you want the actual menu item to say, such as "Open bash as Admin here"
- Optionally, if you want the new item to only appear if you held shift while right-clicking, add to the subkey a String registry value called "Extended"
- Create a sub-subkey called "command" (e.g. HKCR\Directory\shell\AdminBash\command)
- Change the default value of the new sub-subkey to
powershell -c start -verb runas cmd '/c start /D "%V" bash.exe'
- If it doesn't show up immediately, re-launch Windows Explorer (one easy way to do this is log out and in again)
Using a shortcut (.lnk)
This lets you create a file that can be located anywhere but launches bash, as admin, to a target location.
- Right-click on the Desktop or in any Windows Explorer directory (not on a file) and select New -> Shortcut
- Set the destination as
powershell -c start -verb runas cmd '/c start /D "<tartget>" bash.exe'
with replaced by where you want it to open to. - Set any other properties you want, like the file name, icon, and/or shortcut key.
Using a batch (.cmd or .bat) file:
This lets you drop a file wherever you want it that will launch bash as Admin in that location when double-clicked (if opened from a Windows shell, it will instead inherit that shell's location, but still as admin; this might be useful if you add it to a directory in your Windows PATH).
- Create a batch file (this can be done using the Windows Explorer context menu as above for a text file and changing the extension, or using any text editor)
- Set the file's contents to the following:
powershell -c start -verb runas cmd '/c start /D "%CD%" bash.exe'
My "Bash on Ubuntu on Windows" application is pinned to my task bar. All I had to do was as follows:
Right-click on the "Bash on Ubuntu on Windows" application and select Properties.
Under the Shortcut tab, click Advanced.
Select the Run as administrator checkbox and apply the changes.
That's it!