Windows – What kind of action is a “Run” command and how to automate them

run-dialogwindowswindows 10

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

Best Answer

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.

Related Question