For our BGInfo template I searched high and low for the best way to detect the Windows OS Bits (bit-ness) to display on the destop background. Here is some script code that does the same thing - the implementation is uber-simple via some WMI code techniques you many not have seen before.

Our $9.99 eBook/eClass Bundle brings you up to speed on all the challenges of supporting applications on 64-bit Windows. Check it out.

There are a variety of ways to attempt to retreive the OS Bits. Classic methods include retreiving the PROCESSOR_ARCHITECTURE variable and examining it for "AMD64" or "IA64" for 64 bit (or the variables registry key HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE). Checking the environment variable is sometimes unreliable depending on whether it is executed in the 32-bit subsystem, in which case it may report a 64-bit machine as being 32-bit.

The supplied sample code provides functionality for all the possibilities as well as many comments and usage examples:

CSI_GetBitness("OS") - uses reliable WMI based method to get the bit-ness of the OS, regardless of the bit-ness of the Hardware or the 32-bit Subsystem.

CSI_GetBitness("Hardware") - uses reliable WMI based method to get the bit-ness of the Hardware, regardless of the bit-ness of Windows or the 32-bit Subsystem.

CSI_GetBitness("Process") - uses the environment variable to get the bit-ness of the Process, regardless of the bit-ness of Windows or the Hardware. If CSI_GetBitness("OS") is 64, but CSI_GetBitness("Process") is 32, then we know that our script is running in the 32-bit subsystem of 64-bit Windows.

We've reduced the WMI code to exactly one line to retrieve the needed data. This WMI approach works when you can know the exact instance of a WMI class that you wish to retrieve. In this case there could be multiple CPUs, but they would not individually have different bit architecture on the same physical machine.

Simply wrapping this one liner in a function makes it even easier to use in scripts (the code in the function should be on one line, though it may not appear this way in your browser):