Script to associate an extension to a program

The real association is stored in the key that "HKEY_CLASSES_ROOT\.xml" points to.

On my machine, the default value of that key says "xmlfile", most likely that is the same for yours.

So let’s go to "HKEY_CLASSES_ROOT\xmlfile". There you can see (and change) what command is going to be used to launch that type of file:

HKEY_CLASSES_ROOT\xmlfile\shell\open\command

Windows uses this kind of redirection to map multiple file extensions to the same file type, and thus to the same application.

Under "HKEY_CLASSES_ROOT\xmlfile\shell" there are multiple sub-keys that resemble the “verbs” of what you can do to the file. Again, the default value of the "shell" key decides which of these verbs is used if you double click the file. In my case this is "open".

Conclusion:

With that knowledge, the easiest way to make an association scriptable is to use regedit to export a .reg file containing that change, and apply it to the target computer with a double click or:

regedit /s new_xml_association.reg

or (if you are on XP or higher and know what you do) overwrite the current value with:

reg add "HKEY_CLASSES_ROOT\xmlfile\shell\open\command" /ve /d "path\to\program %1"

At any rate, a deeper look into reg add/? command is advised. The first solution is safer.

Leave a Comment