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'
1You can make another batch file in the folder that basically says Line 1:
C:\> runas /user:<DomainName>\<AdministratorAccountName> cmd
then on line 2:cd C:\the_folder_i_want_to_be_in
– Narzard – 2016-05-16T14:18:30.343I didn't get your suggestion completely @Narzard. From where shall I launch that batch file containing those two commands? From normal bash shell which is running without administrative privileges? – RBT – 2018-07-09T12:25:13.877
@RBT Right, so, just open notepad and make the above 2 lines. Then save it as a .bat file. Everytime you use that bat, it will open whatever folder you chose on line 2 as administrator. – Narzard – 2018-07-09T13:29:54.070
@Narzard but the question here is to open a bash shell at the desired path, not a command shell. – RBT – 2018-07-09T14:36:41.257