|CSI_GetBitness One Liner Script to Detect 32-bit or 64-bit Windows|
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.
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:
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):
CSI_GetOSBits = GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'").AddressWidth
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.