What kind of action is a "Run" command and how to automate them?

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


Solution 1:

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.

Solution 2:

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"