How to create a "run as admin" bash shortcut

7

4

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?

drake035

Posted 2016-05-16T08:43:04.500

Reputation: 841

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.343

I 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

Answers

7

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:

  1. Open regedit and navigate to HKEY_CLASSES_ROOT\Directory\shell
  2. Add a new subkey, call it something like "AdminBash"
  3. 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"
  4. 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"
  5. Create a sub-subkey called "command" (e.g. HKCR\Directory\shell\AdminBash\command)
  6. Change the default value of the new sub-subkey to powershell -c start -verb runas cmd '/c start /D "%V" bash.exe'
  7. 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.

  1. Right-click on the Desktop or in any Windows Explorer directory (not on a file) and select New -> Shortcut
  2. 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.
  3. 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).

  1. 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)
  2. Set the file's contents to the following: powershell -c start -verb runas cmd '/c start /D "%CD%" bash.exe'

CBHacking

Posted 2016-05-16T08:43:04.500

Reputation: 5 045

This is a great answer, thanks. I opted for your first suggestion of adding it to the context menu. However, it does not work entirely as you explain it. My option only shows up if I use shift+right-click, even though I did not add the extended sub-subkey. Also the powershell that opens is not elevated ((whoami /all | select-string S-1-16-12288) -ne $null returns false) and it does not run bash upon opening. Any idea what the difference might be? Changed behaviour after windows 10 updates? – Leo – 2017-09-03T08:46:22.093

Works great though how would you implement for directory names with spaces when using the Windows Explorer folder context menu method? – ledragon – 2018-08-15T12:52:52.830

@ledragon The quotation marks around the %V (that is the "%V") mean that the directory path will be treated as a single parameter, even if it has spaces in it. – CBHacking – 2018-08-16T03:21:31.623

@CBHacking - Thanks for the reply. Unfortunately that's not working for me. The DIR name is cut off at the first space and I get the message: Windows cannot find <DIR Name>. Make sure you've typed the name correctly and try again – ledragon – 2018-08-20T11:23:51.470

@CBHacking - The following is the non-admin gitbash link. "C:\Program Files\Git\git-bash.exe" "--cd=%1" - Does that help? – ledragon – 2018-08-20T11:39:58.963

you can also add a string value to the key named "Icon" to add a icon to the bash. e.g. with value: C:\Program Files\WindowsApps\CanonicalGroupLimited.UbuntuonWindows_1804.2018.817.0_x64__79rhkp1fndgsc\ubuntu.exe,0 – cyptus – 2018-11-13T15:27:48.783

1

My "Bash on Ubuntu on Windows" application is pinned to my task bar. All I had to do was as follows:

  1. Right-click on the "Bash on Ubuntu on Windows" application and select Properties.

  2. Under the Shortcut tab, click Advanced.

  3. Select the Run as administrator checkbox and apply the changes.

That's it!

Eric

Posted 2016-05-16T08:43:04.500

Reputation: 19

0

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:

  1. Edit your .bashrc file. 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 .bashrc
    
  2. Next press a and 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.

  1. Save the changes. (by pressing the ESC key, writing :wq and then pressing the ENTER key).

  2. Restart Bash.

Nadav

Posted 2016-05-16T08:43:04.500

Reputation: 101

Who said anything about git-bash? This does not work for the bash built into Win10. – CBHacking – 2017-03-13T10:32:19.180

Sorry about that. I edited my answer to reflect bash for windows 10. – Nadav – 2017-03-30T06:53:34.103

-1

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:

  1. Right click, create shortcut.
  2. 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).
  3. Confirm shortcut creation.
  4. Open the properties of the shortcut.
  5. Set the path where the command should run.
  6. In the advanced options you can enable running as Administrator
  7. Apply/Confirm as needed.

le_top

Posted 2016-05-16T08:43:04.500

Reputation: 274

I think he may be running the insider preview of Windows 10, which natively supports bash. Anyway, the solution might as well be the same. See: https://blogs.windows.com/windowsexperience/2016/04/06/announcing-windows-10-insider-preview-build-14316/

– Marcel N. – 2016-05-16T19:38:50.827

Programs launched using a "launch as Admin" shortcut inherit C:\Windows\System32 as their working directory, no matter what the shortcut says, at least in my build on Win10. Also, a Windows user wouldn't specify "bash" unless they meant "bash", and the question is tagged "windows-10" (which has a Linux subsystem that runs real Ubuntu bash). – CBHacking – 2017-03-13T10:14:33.987

>

  • I just verified on my Windows 10 computer by following my procedure. The target program clearly starts in the directory specified during the procedure and with administrator privilege. - 2. "Cygwin bash" is also real bash. - 3. The question was asked about 1 year ago, at that time it was clearly more logical to suppose the user was running Cygwin - this supposition is clearly stated in the reply. - 4. I can't see why my answer required a "down-vote".
  • < – le_top – 2017-03-13T14:00:43.073