12

How do i detect the bitness (32-bit vs. 64-bit) of the Windows OS in VBScript?

I tried this approach but it doesn't work; I guess the (x86) is causing some problem which checking for the folder..

Is there any other alternative?

progFiles="c:\program files" & "(" & "x86" & ")"

set fileSys=CreateObject("Scripting.FileSystemObject")

If fileSys.FolderExists(progFiles) Then    
   WScript.Echo "Folder Exists"    
End If
3per
  • 351
  • 9
  • 26
Santhosh
  • 6,547
  • 15
  • 56
  • 63
  • No, I think he wants to find out if he is running on a 32 or 64 bit OS. Therefore a duplicate of http://stackoverflow.com/questions/191873 – Treb Aug 27 '10 at 11:23
  • possible duplicate of [Determining 64-bit vs. 32-bit Windows](http://stackoverflow.com/questions/191873/determining-64-bit-vs-32-bit-windows) – Treb Aug 27 '10 at 11:23
  • @Treb: There's no VBScript answer. On second thought, it's probably a duplicate of http://stackoverflow.com/questions/556283/how-do-i-check-if-wscript-cscript-runs-on-x64-host-os – Helen Aug 27 '10 at 11:29

10 Answers10

23

Came up against this same problem at work the other day. Stumbled on this genius piece of vbscript and thought it was too good not to share.

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

Source: http://csi-windows.com/toolkit/csi-getosbits

Bruno
  • 5,772
  • 1
  • 26
  • 43
16

You can query the PROCESSOR_ARCHITECTURE. A described here, you have to add some extra checks, because the value of PROCESSOR_ARCHITECTURE will be x86 for any 32-bit process, even if it is running on a 64-bit OS. In that case, the variable PROCESSOR_ARCHITEW6432 will contain the OS bitness. Further details in MSDN.

Dim WshShell
Dim WshProcEnv
Dim system_architecture
Dim process_architecture

Set WshShell =  CreateObject("WScript.Shell")
Set WshProcEnv = WshShell.Environment("Process")

process_architecture= WshProcEnv("PROCESSOR_ARCHITECTURE") 

If process_architecture = "x86" Then    
    system_architecture= WshProcEnv("PROCESSOR_ARCHITEW6432")

    If system_architecture = ""  Then    
        system_architecture = "x86"
    End if    
Else    
    system_architecture = process_architecture    
End If

WScript.Echo "Running as a " & process_architecture & " process on a " _ 
    & system_architecture & " system."
Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
  • I have to add a quite important note to this... If you run a 32-bit CMD shell and you run the 64-bit wscript/cscript executables inside that shell, this check in your code correctly returns `x86`. HOWEVER if you are going to load COM DLLs or similar inside your code, the host loads them as if they were 64-bit DLLs. I spent four hours banging my head against this before i tested the command manually and it worked, and i knew something was off. – henry700 Jul 04 '16 at 04:03
6

Here is a pair of VBScript functions based on the very concise answer by @Bruno:

Function Is32BitOS()
    If GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'").AddressWidth _
       = 32 Then
        Is32BitOS = True
    Else
        Is32BitOS = False
    End If
End Function

Function Is64BitOS()
    If GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'").AddressWidth _
       = 64 Then
        Is64BitOS = True
    Else
        Is64BitOS = False
    End If
End Function

UPDATE: Per the advice from @Ekkehard.Horner, these two functions can be written more succinctly using single-line syntax as follows:

Function Is32BitOS() : Is32BitOS = (GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'").AddressWidth = 32) : End Function

Function Is64BitOS() : Is64BitOS = (GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'").AddressWidth = 64) : End Function

(Note that the parentheses that surround the GetObject(...) = 32 condition are not necessary, but I believe they add clarity regarding operator precedence. Also note that the single-line syntax used in the revised implementations avoids the use of the If/Then construct!)

UPDATE 2: Per the additional feedback from @Ekkehard.Horner, some may find that these further revised implementations offer both conciseness and enhanced readability:

Function Is32BitOS()
    Const Path = "winmgmts:root\cimv2:Win32_Processor='cpu0'"
    Is32BitOS = (GetObject(Path).AddressWidth = 32)
End Function

Function Is64BitOS()
    Const Path = "winmgmts:root\cimv2:Win32_Processor='cpu0'"
    Is64BitOS = (GetObject(Path).AddressWidth = 64)
End Function
DavidRR
  • 18,291
  • 25
  • 109
  • 191
  • 1
    Poeple paid by LOC or hour will like this; all other will recognize "Function F() : If Condition Then F = True Else F = False : End Function" as adiposity. – Ekkehard.Horner Dec 20 '12 at 20:23
  • The lean version would be: "Function F() : F = Condition : End Function" - no ternary operator, no pondering whether the assignments are switched, and 4 lines less. – Ekkehard.Horner Dec 20 '12 at 21:29
  • A conditional expression (e.g. "GetObject(...).AdressWidth = 64" evaluates to a boolean value that can be assigned without further ado to the function name (VBScript's way of return something from a function). – Ekkehard.Horner Dec 20 '12 at 22:02
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/21484/discussion-between-ekkehard-horner-and-davidrr) – Ekkehard.Horner Dec 20 '12 at 22:40
  • 1
    +1 for the lean version (which is just as lean if you use 3 lines; the point is the direct assignment of the conditional expression('s result) to the function name). – Ekkehard.Horner Dec 21 '12 at 19:31
5

WMIC queries may be slow. Use the environment strings:

Function GetOsBits()
   Set shell = CreateObject("WScript.Shell")
   If shell.ExpandEnvironmentStrings("%PROCESSOR_ARCHITECTURE%") = "AMD64" Then
      GetOsBits = 64
   Else
      GetOsBits = 32
   End If
End Function
Mustafa Burak Kalkan
  • 1,132
  • 21
  • 28
4

Determining if the CPU is 32-bit or 64-bit is easy but the question asked is how to determine if the OS is 32-bit or 64-bit. When a 64-bit Windows is running, the ProgramW6432 environment variable is defined.

This:

CreateObject("WScript.Shell").Environment("PROCESS")("ProgramW6432") = ""

will return true for a 32-bit OS and false for a 64-bit OS and will work for all version of Windows including very old ones.

Regis Desrosiers
  • 537
  • 3
  • 13
  • so if you run vbs by itself it would use 32-bit script by default so you have to use C:\Windows\Syswow64\cscript to run this and then it would return 64-bit – SeanClt May 11 '18 at 18:20
  • You bring a good point. Honestly I used this technique and it worked well on Windows 2000, XP 32-bit, Windows 7, 8 and 10 but I have not tested it on all OSes. So ... it is a simple solution that may not work on all version of Windows like Server 2003. – Regis Desrosiers May 11 '18 at 23:22
  • I found a simple solution just look to C:\windows\syswow64\cacript exists if it doesn’t it’s 32bit – SeanClt May 13 '18 at 01:33
  • 1
    @seanClt: `%windir%\SysWow64\CScript` exists... except when you are running on a stripped version of x64 Windows where the WoW64 system is not present: like Windows PE, Server Core when `WoW64-Support` is removed, etc. – AntoineL May 13 '20 at 07:46
3

Addendum to Bruno's answer: You may want to check the OS rather than the processor itself, since you could install an older OS on a newer CPU:

strOSArch = GetObject("winmgmts:root\cimv2:Win32_OperatingSystem=@").OSArchitecture

Returns string "32-bit" or "64-bit".

amonroejj
  • 573
  • 4
  • 16
2

You can also check if folder C:\Windows\sysnative exist. This folder (or better alias) exist only in 32-Bit process, see File System Redirector

Set fso = CreateObject("Scripting.FileSystemObject")
Set wshShell = CreateObject( "WScript.Shell" )

If fso.FolderExists(wshShell.ExpandEnvironmentStrings("%windir%") & "\sysnative" ) Then
    WScript.Echo "You are running in 32-Bit Mode"
Else
    WScript.Echo "You are running in 64-Bit Mode"
End if

Note: this script shows whether your current process is running in 32-bit or 64-bit mode - it does not show the architecture of your Windows.

Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110
0
' performance should be good enough
' Example usage for console:
' CSript //NoLogo *ScriptName*.vbs
' If ErrorLevel 1 Echo.Win32
' VBScript:
On Error Resume Next
Const TargetWidth = 32
Set WMI = GetObject("winMgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set Query = WMI.ExecQuery("SELECT AddressWidth FROM Win32_Processor")
For Each Item in Query
  If Item.AddressWidth = TargetWidth Then
    WScript.Quit 1
  End If
Next
WScript.Quit 0
  • While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Neo Anderson Sep 15 '20 at 19:26
0

Using environment. Tested in XP, but I can't find a 32 bit CPU to test...

   function getbitsos()
      with WScript.CreateObject("WScript.Shell").environment("PROCESS")
        if .item("PROCESSOR_ARCHITECTURE") ="X86" and .item("PROCESSOR_ARCHITEW6432") =vbnullstring Then
          getbitsos=array(32,32,32)
        elseif .item("PROGRAMFILES(x86)")=vbnullstring Then 
          getbitsos=array(64,32,32)
        elseif .item("PROGRAMFILES(x86)")=.item("PROGRAMFILES") Then
          getbitsos=array(64,64,32)
        Else
          getbitsos=array(64,64,64)
        end if   
      end with
    end function
    
    a=getbitsos()
    wscript.echo "Processor " &a(0) & vbcrlf & "OS "  & a(1) &vbcrlf& "Process " & a(2)& vbcrlf 
Antoni Gual Via
  • 714
  • 1
  • 6
  • 14
0

This is the same proposed solution in Microsoft blog https://learn.microsoft.com/en-us/archive/blogs/david.wang/howto-detect-process-bitness

tested in XP 32 and win7 64 (using a 32 bit and 64 bit CMD)

Set objShell = CreateObject("WScript.Shell")
os_bit = 64
arch = objShell.ExpandEnvironmentStrings("%PROCESSOR_ARCHITECTURE%")
archW6432 = objShell.ExpandEnvironmentStrings("%PROCESSOR_ARCHITEW6432%")
If LCase(arch) = "x86" Then
    If archW6432 = "" Or LCase(archW6432) = "%processor_architew6432%" Then
        os_bit = 32
    End If
End If

WScript.Echo "Operating System Bit: " & os_bit
Badr Elmers
  • 1,487
  • 14
  • 20