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?
44 Answers
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
regeditand 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!
I'll assume you are comfortable working with vim, and will use it when providing the answer. You can configure Bash for Windows to navigate by default to a specific directory (folder) by doing the following:
Edit your
.bashrcfile. This must be done from within Bash because you need to edit the file via a linux-based app. Editing this file from within a Windows app will most likely cause it to become corrup.In Bash type the following:cd ~ vim .bashrcNext press
aand append the following line to the end of the file:cd /mnt/c/yourFolder/
Note that this example assumes that your file is under the C drive and in a folder called yourFolder.
Save the changes. (by pressing the
ESCkey, writing:wqand then pressing theENTERkey).Restart Bash.
I suppose that you mean to write "shell" or "cmd" instead of "bash" because the latter would suggest that you are running a windows version of the bash shell which is a *nix shell (I use Cygwin for that).
Either case, you can create a shortcut in the usual way. That is:
- Right click, create shortcut.
- Enter the executeable for the shortcut - either 'cmd' for the standard windows shell, or c:\cygwin\bin\bash if you are actually using bash. for the standard shell, you can just type "cmd" for the location (no need to enter the full path).
- Confirm shortcut creation.
- Open the properties of the shortcut.
- Set the path where the command should run.
- In the advanced options you can enable running as Administrator
- Apply/Confirm as needed.