Using environment variables in registry strings.

As part of my job, I receive zip files which I want to do some processing on. I wrote a powershell script to do this processing for me and it’s been really useful. However, even having to fire up Powershell and type in the commands to execute it has become a complete chore (I’m a very lazy person!).

So I decided to add a context-menu item to Windows Explorer for .zip files which would run the script for me.

My initial script looked like this, but had a couple of flaws:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\.zip\shell\PS Import]
@="Do Powershell Import"

[HKEY_CLASSES_ROOT\.zip\shell\PS Import\command]
@="%SystemRoot%\\system32\\WindowsPowerShell\\v1.0\\powershell.exe \"& '%User_Profile%\\Documents\\my_script.ps1' '%1'\""

The first issue I came across was that I was adding the registry keys to HKEY_CLASSES_ROOT\.zip. This turned out to be incorrect. The correct location was HKEY_CLASSES_ROOT\CompressedFolder.

The second issue is that the default value for a registry key is a REG_SZ (a registry string). This would have been fine, except that I was using environment variables (%SystemRoot% and %UserProfile%) in my path. It turns out that these don’t get expanded at runtime.

Instead, I would need to make the default value for the key of type REG_EXPAND_SZ, which is a registry string which will expand variables.

So my new script looks like this:


Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\CompressedFolder\shell\PS Import]
@="Do Powershell Import"

[HKEY_CLASSES_ROOT\CompressedFolder\shell\PS Import\command]
@=hex(2):25,00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,74,00,\
25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,5c,00,57,00,69,\
00,6e,00,64,00,6f,00,77,00,73,00,50,00,6f,00,77,00,65,00,72,00,53,00,68,00,\
65,00,6c,00,6c,00,5c,00,76,00,31,00,2e,00,30,00,5c,00,70,00,6f,00,77,00,65,\
00,72,00,73,00,68,00,65,00,6c,00,6c,00,2e,00,65,00,78,00,65,00,20,00,22,00,\
26,00,20,00,27,00,25,00,55,00,73,00,65,00,72,00,5f,00,50,00,72,00,6f,00,66,\
00,69,00,6c,00,65,00,25,00,5c,00,44,00,6f,00,63,00,75,00,6d,00,65,00,6e,00,\
74,00,73,00,5c,00,6d,00,79,00,5f,00,73,00,63,00,72,00,69,00,70,00,74,00,2e,\
00,70,00,73,00,31,00,27,00,20,00,27,00,25,00,31,00,27,00,22,00,00,00

Obviously, this is not directly maintainable. The only way I can maintain it is to edit the string in the registry, then export the shell key to a new reg file.

The final issue I found was when I was writing this post at home. Here, I have WinRar installed, so I also needed to add the same keys to HKEY_CLASSES_ROOT\WINRAR.ZIP\shell, and I added it into HKEY_CLASSES_ROOT\.zip\shell for good luck. If anyone can explain a little bit about how Windows goes about looking up the context menu in the registry for a given extension, I’d really appreciate it!