8

My main questions within the context below:

  1. What are "Run" commands from Windows' perspective?
  2. How to automate "Run" commands?
  3. Is "Run" similar to the address bar of the Windows Explorer?


As many of you might have noticed, you have to make a couple of clicks to get to the Control Panel's Sound Properties menu in Windows 10.

  • Instead of having to search for links in descriptive words within Settings - Sound:
    Right-clicking taskbar speaker icon > Sound settings > Settings: Sound
  • You could (where option is 0, 1, 2, or 3 for every for every tab in that menu):
    WinKey+R > mmsys.cpl || control mmsys.cpl,,[option]

This made me think of how to automate the above by creating a batch on my taskbar (example), but I'm unsure of what commands to use. You can easily run scripts in batch files, but it's not the same thing and I can't find anywhere how to make a batch file that runs when you type its name in "Run" (like cmd, sysedit, regedit, msconfig, etc.)

  • 3
    I think there's a bit of confusion over what you want. Are you trying to create a script which, when double-clicked, runs something as if you typed it in the "Run" dialog? Or are you trying to create a custom command which you can type in the "Run" dialog, and will run your choice of command? – IMSoP Dec 23 '19 at 10:33
14

LPChip's excellent answer addresses how to do what you want from batch files, but you could also just create a shortcut of desktop to run exactly the same command that you would type in a "Run..." dialogue:

  1. Right click on your desktop and select New -> Shortcut.
  2. Type the command exactly as you do it in the "Run..." dialogue where it says "Type the location of the item:". To run the example from your question, you could enter control mmsys.cpl,,2. Click "Next".
  3. Change the name of the shortcut if you want and click "Finish".

Create shortcut for a command

What "Run..." does

If you want a more "technical" answer as to what the "Run..." dialogue does, it calls a system API function (most likely ShellExecuteEx) which does roughly the following:

  • Resolves file associations, so it knows which program to open a document with if you give it path that is not an executable, but a document -- say, a jpeg image or a word document.
  • Resolves and uses environment variables such as PATH or HOMEPATH. If you type %HOMEPATH% in the "Run..." dialogue, it will open your user's profile folder. And PATH is a special system environment variable which contains a list of default locations in which Windows will try to look for the programs. This is why you're able to type just control and not use the full path for control.exe (which is C:\Windows\System32\control.exe).
  • As @IMSoP mentioned in the comments, in addition to the PATH environment variable, another place that is checked for executable paths is the registry. More specifically, HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths for system-wide paths, and HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\App Paths for those specific to the current user.
  • Passes the additional command line arguments to the program. In your example control mmsys.cpl,,[option], control is the the file that gets run, and mmsys.cpl,,[option] is the command line parameter.

It seems that you are correct in your assumption that the address bar does the same thing, except it tries to navigate to the path in case it's a folder it could display.

Command line command start from LPChip's answer is the way to do the same thing in cmd or a batch script.

Also opening a shortcut does roughly the same thing.

For those interested in the complete picture of what's going on, I suggest reading this article on MSDN on application registration, which provides a comprehensive overview of how it all works.

  • There are actually some features specific to the Run dialog, including some extra registry lookups, so this guess doesn't do the question justice. For instance, if you have Microsoft Word installed, try typing winword at the Run prompt, and compare typing it in a command shell. – IMSoP Dec 23 '19 at 10:29
  • 2
    @IMSoP, that's a nice guess you took there about registry lookup being specific to "Run..." ;) However, it is not. It works it every place that uses the same underlying API: start command in cmd, Explorer address bar, shortcut path, and yes, the "Run..." dialogue. – Alexander Revo Dec 23 '19 at 12:16
  • Thanks for pointing out the omission of the registry keys though, added the info to the answer, plus a link to an MSDN article with a complete description of the process. – Alexander Revo Dec 23 '19 at 12:31
19

Batchfiles are just a list of instructions similar as to what you type in Command Prompt.

In command prompt, you can run an application with .exe or .com simply by typing its filename.

For example, if you have the file example.exe in the current folder, you can just type in example in command prompt to start that file. The same can also be done in a batch file.

Simply place example in a line to start that file.

But when you have a file that doesn't have an .exe or .com extension, the run dialog will still launch it, but command prompt will not if you just type in the name of the file.

So from the run dialog, mmsys.cpl will work, whereas in command prompt it will not.

The reason for this is because Explorer is capable of understanding file association whereas command prompt is not. So to compensate for this, Microsoft added the command start.

If you type start filename.ext in command prompt, the file is run using explorer's engine, and as such whatever is associated to that file extension will start the file. .cpl is associated directly with explorer to open the control panel's dialog.

So in your batchfile, you can simply type:

start mmsys.cpl

And if you started to think, yes, you can launch a word document directly from Command Prompt into word by typing start "My Awesome Worddocument.docx"

  • 1
    I'm sorry but I was not asking about batch files. I was asking about "Run" commands and how to automate those (if possible)... – newbie Dec 23 '19 at 1:25
  • 9
    @newbie I'm explaining exactly that. To "run" something from batch, the way the Run dialog does it, you use the command directly or through start. Keep in mind, Command Prompt is very limited, such as that it can't send keypresses etc unless you use additional tools, but other scripts can be run if you really need that. – LPChip Dec 23 '19 at 7:43
  • 17
    @newbie I am sorry, but it is you who doesn't understand how this works. There are no "run commands". There are several different Windows executable file types, and "run" is but a small window which allows you to manually enter their names. If you want to create an icon running such a command, you either create a batch file as described in this answer and make a shortcut to it, or you can install some macro engine which allows you to simulate keypresses and button clicks. That, in this case, would seem to be an error prone overkill. – Gnudiff Dec 23 '19 at 7:48
  • 2
    @Gnudiff Actually, there are specific behaviours of the "Run" dialog not related to file associations, but they are also available with the start command. Try typing winword at a command prompt, and then try start winword; the lookup is actually against a registry key. – IMSoP Dec 23 '19 at 10:35
  • @IMSoP Good point, I didn't know that; nevertheless it seems that it doesn't invalidate the answer. – Gnudiff Dec 23 '19 at 11:22
0

What you need is the start-process command in powershell. Consider the following, where start is used as an alias for start-process.

PS> start excel
PS> start myfile.docx
PS> start http://www.website.com

In the first case, the command names a program to be run. In the second case, the command names a file, and windows uses the registry to discover the appropriate program. In the third case, the command names a URL, and windows runs a browser.

Powershell is on the way to replacing both Batch and DOS commands. You can put commands like these in powershell scripts. Now all you have to do is make a script run in response to some triggering event.

  • 2
    -1 because he doesn't need this, and it does exactly the same as the start command in batch scripting/command prompt. In addition it is far easier to implement this in batch and lastly, OP asks for something that is not doing additional scripting. Powershell falls under that last category. – LPChip Dec 23 '19 at 20:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.