How can I use VBScript to determine whether I am running a 32-bit or 64-bit Windows OS?
Asked Answered
K

10

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
Knuth answered 27/8, 2010 at 11:16 Comment(3)
No, I think he wants to find out if he is running on a 32 or 64 bit OS. Therefore a duplicate of stackoverflow.com/questions/191873Altorelievo
possible duplicate of Determining 64-bit vs. 32-bit WindowsAltorelievo
@Treb: There's no VBScript answer. On second thought, it's probably a duplicate of #556783Evoke
I
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."
Innerve answered 27/8, 2010 at 11:31 Comment(1)
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.Walloper
S
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

Signal answered 13/10, 2011 at 21:6 Comment(1)
+1.49 - nice solution - but - alas - works on WMI time (measured in cups of tea).Erbil
I
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."
Innerve answered 27/8, 2010 at 11:31 Comment(1)
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.Walloper
T
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
Tysontyumen answered 20/12, 2012 at 19:59 Comment(5)
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.Erbil
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.Erbil
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).Erbil
let us continue this discussion in chatErbil
+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).Erbil
N
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
Nims answered 8/11, 2015 at 6:37 Comment(0)
S
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.

Sweated answered 12/1, 2018 at 13:0 Comment(4)
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-bitKortneykoruna
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.Sweated
I found a simple solution just look to C:\windows\syswow64\cacript exists if it doesn’t it’s 32bitKortneykoruna
@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.Puca
I
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".

Intuitivism answered 4/10, 2016 at 14:53 Comment(2)
Addendum to Addendum: .OSArchitecture doesn't exist on XP.Intuitivism
It can return "64 bits", maybe "64 bit", depending on the localization.Linguiform
S
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.

Smart answered 27/8, 2018 at 12:0 Comment(2)
sysnative does not exist in non-updated 64-bit XP/2003.Appledorf
How old is XP/2003? 20 years?Smart
D
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
Derouen answered 15/9, 2020 at 16:37 Comment(1)
While this code snippet may be the solution, including an explanation 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.Hoicks
Y
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 
Younglove answered 23/11, 2021 at 15:45 Comment(0)
E
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
Enplane answered 22/8, 2023 at 4:0 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.