how to get program files x86 env variable?
Asked Answered
G

5

126

I would like to know how I can display the location of Program Files (x86) in Command Prompt. I'm using Windows 7 64bit.

I've tried:

echo %programfiles(x86)% and echo %programfiles%.

Both of these output C:\Program Files

When I manually checked the registry,
HKLM/Software/microsoft/windows/currentversion,
the programfilesdir points to C:\Program Files and

HKLM/Software/WOW64/Microsoft/windows/currentversion,
the programfilesdir points to C:\Program Files (x86).

But, why am I always seeing C:\Program Files displayed?

Galven answered 7/3, 2012 at 0:26 Comment(2)
I think the real question is, why isn't there a version of %programfiles% that always points to x86 on both windows 7 and windows xp to simplify running programs that are installed on both? For example, after installing Debugging Tools for Windows (x86) on XP, it's found in Program Files but on Windows 7 it's found on Program Files (x86) which means there's no simple way to create a command file that can be distributed across all computers since none of the built-in environment variables consistently point to the 32-bit location for Program Files.Landloper
[posted after reading all answer] IMHO, one point that is missing in this discussion is that whatever variable you use, it is guaranteed to always point at the appropriate folder. This becomes critical in the rare cases where Windows is installed on a drive other than C:\.Woolsey
J
213

On a 64-bit machine running in 64-bit mode:

  • echo %programfiles% ==> C:\Program Files
  • echo %programfiles(x86)% ==> C:\Program Files (x86)

On a 64-bit machine running in 32-bit (WOW64) mode:

  • echo %programfiles% ==> C:\Program Files (x86)
  • echo %programfiles(x86)% ==> C:\Program Files (x86)

On a 32-bit machine running in 32-bit mode:

  • echo %programfiles% ==> C:\Program Files
  • echo %programfiles(x86)% ==> %programfiles(x86)%
Joist answered 7/3, 2012 at 20:45 Comment(5)
What %programfiles(x86)% will return on 32-bit machine in 32-bit mode?Boyette
On windows XP (x86) doesn't work, you need put %programfiles%. I think the only way is to check by code the OS version first and then use one variable or other.Hyoscyamine
It might be worth adding %ProgramW6432% to the list above.Version
The same applies to 32-bit version of Windows 7 - there is no %programfiles(x86)% environmental variableOutdo
I second @Alex Wiese, %ProgramW6432% will have the 64-bit program files directory, always. %ProgramFiles(x86)% will have the 32-bit program files directory, always. Those two variables are stable... the others may change depending on what mode the 64-bit OS (or command prompt) is running in.Anastasiaanastasie
A
42

Another relevant environment variable is:

%ProgramW6432%

So, on a 64-bit machine running in 32-bit (WOW64) mode:

  • echo %programfiles% ==> C:\Program Files (x86)
  • echo %programfiles(x86)% ==> C:\Program Files (x86)
  • echo %ProgramW6432% ==> C:\Program Files

From Wikipedia:

The %ProgramFiles% variable points to the Program Files directory, which stores all the installed programs of Windows and others. The default on English-language systems is "C:\Program Files". In 64-bit editions of Windows (XP, 2003, Vista), there are also %ProgramFiles(x86)%, which defaults to "C:\Program Files (x86)", and %ProgramW6432%, which defaults to "C:\Program Files". The %ProgramFiles% itself depends on whether the process requesting the environment variable is itself 32-bit or 64-bit (this is caused by Windows-on-Windows 64-bit redirection).

Reference: http://en.wikipedia.org/wiki/Environment_variable

Avner answered 31/12, 2014 at 12:7 Comment(2)
Better reference: MSDN: WOW64 Implementation Details - "The ProgramW6432 and CommonProgramW6432 environment variables were added starting with Windows 7 and Windows Server 2008 R2." Wikipedia directly contradicts this; interestingly, Wikipedia only lists the three versions that don't support this variable according to MSDN. Unfortunately I don't have 64-bit XP/Vista to test.Cowlick
On second read, I see that the Windows 7/2008 R2 requirement only applies to 64-bit processes. The variable is defined only for 32-bit processes on Vista x64.Cowlick
P
5

On a Windows 64 bit machine, echo %programfiles(x86)% does print C:\Program Files (x86)

Polyurethane answered 7/3, 2012 at 0:30 Comment(0)
H
5

On a 64-bit Windows system, the reading of the various environment variables and some Windows Registry keys is redirected to different sources, depending whether the process doing the reading is 32-bit or 64-bit.

The table below lists these data sources:

X = HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion
Y = HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion
Z = HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList
     
READING ENVIRONMENT VARIABLES:    Source for 64-bit process               Source for 32-bit process
-------------------------------|----------------------------------------|--------------------------------------------------------------
                %ProgramFiles% :  X\ProgramW6432Dir                       X\ProgramFilesDir (x86)
           %ProgramFiles(x86)% :  X\ProgramFilesDir (x86)                 X\ProgramFilesDir (x86)
                %ProgramW6432% :  X\ProgramW6432Dir                       X\ProgramW6432Dir
     
          %CommonProgramFiles% :  X\CommonW6432Dir                        X\CommonFilesDir (x86)
     %CommonProgramFiles(x86)% :  X\CommonFilesDir (x86)                  X\CommonFilesDir (x86)
          %CommonProgramW6432% :  X\CommonW6432Dir                        X\CommonW6432Dir
     
                 %ProgramData% :  Z\ProgramData                           Z\ProgramData


      READING REGISTRY VALUES:    Source for 64-bit process               Source for 32-bit process
-------------------------------|----------------------------------------|--------------------------------------------------------------
             X\ProgramFilesDir :  X\ProgramFilesDir                       Y\ProgramFilesDir
       X\ProgramFilesDir (x86) :  X\ProgramFilesDir (x86)                 Y\ProgramFilesDir (x86)
            X\ProgramFilesPath :  X\ProgramFilesPath = %ProgramFiles%     Y\ProgramFilesPath = %ProgramFiles(x86)%
             X\ProgramW6432Dir :  X\ProgramW6432Dir                       Y\ProgramW6432Dir
     
              X\CommonFilesDir :  X\CommonFilesDir                        Y\CommonFilesDir
        X\CommonFilesDir (x86) :  X\CommonFilesDir (x86)                  Y\CommonFilesDir (x86)
              X\CommonW6432Dir :  X\CommonW6432Dir                        Y\CommonW6432Dir
     

So for example, for a 32-bit process, the source of the data for the %ProgramFiles% and %ProgramFiles(x86)% environment variables is the Registry value HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramFilesDir (x86).

However, for a 64-bit process, the source of the data for the %ProgramFiles% environment variable is the Registry value HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramW6432Dir ...and the source of the data for the %ProgramFiles(x86)% environment variable is the Registry value HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramFilesDir (x86)

Most default Windows installation put a string like C:\Program Files (x86) into the Registry value HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramFilesDir (x86) but this (and others) can be changed.

Whatever is entered into these Windows Registry values will be read by Windows Explorer into respective Environment Variables upon login and then copied to any child process that it subsequently spawns.

The registry value HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramFilesPath is especially noteworthy because most Windows installations put the string %ProgramFiles% into it, to be read by 64-bit processes. This string refers to the environment variable %ProgramFiles% which in turn, takes its data from the Registry value HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramW6432Dir ...unless some program changes the value of this environment variable apriori.

I have written a small utility, which displays these environment variables for 64-bit and 32-bit processes. You can download it here.
The source code for VisualStudio 2017 is included and the compiled 64-bit and 32-bit binary executables are in the directories ..\x64\Release and ..\x86\Release, respectively.

Halfblood answered 30/12, 2020 at 1:17 Comment(1)
In Visual Studio, just use [TARGETDIR] in the registry property and it'll be translated during installation into the correct path either Program Files on 32 bit or Program Files (x86) on 64 bit.Darr
W
0

IMHO, one point that is missing in this discussion is that whatever variable you use, it is guaranteed to always point at the appropriate folder. This becomes critical in the rare cases where Windows is installed on a drive other than C:\

Woolsey answered 3/10, 2019 at 14:39 Comment(1)
This is a good note. But since it's not answering the question directly, it would have been better to write it as a comment to the question itself.Merimerida

© 2022 - 2024 — McMap. All rights reserved.