0

I often find myself in need of running a command in msys2 in an elevated cmd shell. How can I quickest open an elevated cmd in the current directory from msys2?

I tried this answer: https://stackoverflow.com/questions/19098101/how-to-open-an-elevated-cmd-using-command-line-for-windows, but it does not seem to work from msys2, with this error:

enter image description here

MyrionSC2
  • 271
  • 3
  • 3
  • Isn't there a SysInternals tool to add context menus? Thinking out loud here... – spikey_richie Oct 24 '19 at 09:40
  • Something like this? https://www.tenforums.com/tutorials/59686-open-command-window-here-administrator-add-windows-10-a.html – spikey_richie Oct 24 '19 at 09:41
  • @spikey_richie That seems like it adds an option to some kind of context menu, which is not exactly what I am after. In msys2 right now, if I run "cmd" It will enter a cmd shell. It is something like that I am after, just for an elevated cmd. I would be okay with it opening in a new shell window as well, as long as it is in the same directory as the msys2 shell. And there probably is no way to get around having to click Yes to the admin popup. I will have to live with that probably. – MyrionSC2 Oct 24 '19 at 10:16
  • I found http://www.nirsoft.net/utils/nircmd.html in another answer. Maybe that is what I am after, but I don't have time to test it right now. – MyrionSC2 Oct 24 '19 at 10:16

1 Answers1

0

Try this:

  • Set the MSYS shell launcher to be opened as Admin by default
  • Set cmd to be opened as Admin by default
  • Inside the MSYS shell use the command

    /bin/start cmd

    and it should open a cmd window as Admin withouth prompt in the directory where the MSYS shell is

You'll still need to view the prompt for opening the MSYS shell as Admin and the cmd window will not be focused(those two things could be pretty annoying but it's better than nothing).
I haven't checked but you can try the steps above withouth forcing Admin opening on the MSYS shell but only on cmd(so you don't have to view the Admin prompt).

Optional tip:

Since you said you use cmd from msys2 shell a lot, you could write a function inside a bash startup file(like .bashrc) to wrap the command like this

opencmd()
{
    /bin/start cmd
}

so you can just type opencmd inside the shell.

EDIT:

1) You actually need to force Admin opening only the MSYS shell and the command will open cmd as Admin

2) I tried the steps on another machine and, after opening cmd with the command, the cmd window was correctly focused(though I still don't know why on the other machine the window wasn't focused)

3) Instead of the command proposed above, you can just type cmd in the MSYS shell and it will open cmd inside the shell as Admin(if you forced the Admin opening of the MSYS shell) but I noticed that

  • the commands typed in cmd are echoed one more time before being executed
  • the cursor can be moved freely inside the shell causing weird behaviour

so be careful if you decide to use this solution

EDIT 2:

  • If you need to execute 1 command from cmd and come back to the MSYS shell, you can just type

    cmd //c your_command

    inside the MSYS shell

  • If you need to execute 1 command and then you want to stay inside the cmd inside the MSYS shell, you can just type

    cmd //k your_command

    inside the MSYS shell

(remember to use //)

Sources:
How to run internal cmd command from the msys shell?
https://ss64.com/nt/cmd.html

AlbyTree
  • 11
  • 3