CSI_GetBitness One Liner Script to Detect 32-bit or 64-bit Windows Print E-mail

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.

Here is the one line required to retrieve Windows OS Bits: 

Bits = GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'").AddressWidth

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): 

  Function CSI_GetOSBits() 

CSI_GetOSBits = GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'").AddressWidth 

  End Function

Here is an example of how scripts that must handle both 32 and 64 bit can be made very simple:

  PathToUtils = "\\server\share\utils\" & CSI_GetOSBits & "-bit\"

The script sample attached to this post includes another function to detect the hardware bits and a couple examples of how it can be used to report the bits or be used in If statements.


12/16/2010 - Version 1.2 - rewrite to use one function for all retrievals, added retrieval of process bit-ness.

Download this file (CSI_GetBitness.zip)CSI_GetBitness.zip[ ]1 Kb