I have a Powershell script on Win10 that needs to start multiple other programs, and finding that in a lot of cases I have to go looking for the full path to the executable file, and potentially hard-code that path in the script, which is not great for robustness or portability between machines.
On *nix systems, it is expected that most programs can be started from the command line, so each application will generally either add its install folder to the PATH environment variable, or be inserted (e.g. via symlinks) into a directory like /usr/bin that's already in the PATH.
Microsoft discourages the use of the PATH variable. Instead applications are SUPPOSED to insert a key for their main .exe file into one of two places in the registry, depending on whether the app is installed for one user or all users-
- HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\App Paths
- HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App Paths
The (default) value under that key is the full path to the executable file.
Except- it seems an awful lot of applications don't follow this recommendation. (Micrsoft's own PowerToys, Keepass2, FreeFileSync, Free Download Manager are just some examples).
These programs do (when not installed as portable apps) have a presence in the Registry, it's just not in the officially recommended places.
So, my question is- Is there an effective way to get a good (not necessarily perfect) general answer to the question "what is the full path to installed program X.exe" on Windows? at least where that program has saved a path somewhere in the registry.
This is similar to the question asked here- Equivalent of *Nix 'which' command in PowerShell? but with the important difference that I want to include "badly behaved" programs that don't correctly configure the PATH or the two registry keys described above.
Ideally this would be a function that could be called on the fly with the program name as a parameter that returns the pathname, but worst case if it requires a brute force search across the whole registry to find and then de-duplicate all the paths to .exe files under the %PROGRAMFILES% folders (64 and 32 bit), I guess I could run this once and save the result in a file as a hash table.