# Solved: wmic os architecture in a batch question



## scrfix (May 3, 2009)

Hello everyone. Long time it has been. I have a question. I have been working on this batch file and am running into a stumbling block:


```
@echo on
For /F "skip=1 delims=-" %%a In ('wmic os get osarchitecture') Do ( 
set _osarch=%%a
echo %%a
)
echo %_osarch%
pause
```
The above returns the following (with my notes in bold):
*This is what I would expect*
C:\>For /F "skip=1 delims=-" %a In ('wmic os get osarchitecture') Do (
set _osarch=%a
echo %a
)

*This is ok. It is setting *
C:\>(
set _osarch=32
echo 32
)
32

*The above is exactly what I want. Now I am not sure why the below is coming up. Even the actual command only returns:
OSArchitecture
32-bit
The t _osarch= doesn't make any sense unless my FOR statement is incorrect.
*
C:\>(
t _osarch=
echo
)
ECHO is on.

*Here the programming gives me two echo blank statements.*
C:\>echo
ECHO is on.

C:\>pause

I have also attempted this as such to no avail:

```
@echo on
Setlocal EnableDelayedExpansion
For /F "skip=1 delims=-" %%a In ('wmic os get osarchitecture') Do ( 
set _osarch=%%a
echo %%a
)
EndLocal&echo %_osarch%
pause
```
I have also added before the EndLocal Set DisableDelayedExpansion. I have tried it with tokens=2 which is what I figured it would be to no avail. I know that I am missing something but I have researched all over for this and cannot figure out what I am missing. I merely only want it to return to a variable that I can use outside of the for loop the value 32 or 64 and nothing else.


----------



## Squashman (Apr 4, 2003)

Why not just pull the information from the environmental variable?

```
H:\>echo %PROCESSOR_ARCHITECTURE%
x86
```


----------



## scrfix (May 3, 2009)

Squashman, I am aware that I can do the Processor Architecture variable. It spits out X86 or AMD_64 based upon the architecture but does not tell me whether the OS is 32 bit or 64 bit hence the osarchitecture which does tell me that.


----------



## scrfix (May 3, 2009)

I figured it out. If anyone else needs this as I saw this asked everywhere, here it is:


```
@echo off
For /F "tokens=2 delims==,-" %%a In ('wmic os get osarchitecture /value') Do ( 
set _osarch=%%a
)
:: Do what you want with %_osarch% now.
:: echo %_osarch% 
pause
```


----------



## Squashman (Apr 4, 2003)

That code does not work on XP Pro 32bit as far as I can tell.

From what I had read yesterday OSarchitecture is a newer variable and does not work on older systems.


----------



## scrfix (May 3, 2009)

I have not had a chance to test it out on other systems. I do have some older systems. I know that it will not work for 98 & ME however I did read prior to implementation that wmic has been installed since windows 2000. Now whether or not the OSArchitecture command within WMIC has been available since then I do not know. I have xp pro systems here now. I will test it out. I do have some IF ELSE statements within my Batch stating that if it could not find it then presume one way or the other. Thank you for pointing that out to me.

*Update* 11:02AM 05-11-11:
Well, sure enough. I ran that code on my XP Pro machines and it failed. So I ran
wmic os get /? and then created a batch file from everything that it does have and here are the results. The most promising looks to be the OSType.

However here is a website where they appear to have a solution for Windows XP. I will try this and see if it is accurate: http://ss64.org/viewtopic.php?id=803

C:\>wmic os get BootDevice
BootDevice
\Device\HarddiskVolume1

C:\>wmic os get BuildNumber
BuildNumber
2600

C:\>wmic os get BuildType
BuildType
Uniprocessor Free

C:\>wmic os get CSDVersion
CSDVersion
Service Pack 3

C:\>wmic os get CSName
CSName
<Computer Name Removed>

C:\>wmic os get CodeSet
CodeSet
1252

C:\>wmic os get CountryCode
CountryCode
1

C:\>wmic os get CurrentTimeZone
CurrentTimeZone
-240

C:\>wmic os get Debug
Debug
FALSE

C:\>wmic os get Description
Description

C:\>wmic os get Distributed
Distributed
FALSE

C:\>wmic os get EncryptionLevel
EncryptionLevel
168

C:\>wmic os get ForegroundApplicationBoost
ForegroundApplicationBoost
2

C:\>wmic os get FreePhysicalMemory
FreePhysicalMemory
324436

C:\>wmic os get FreeSpaceInPagingFiles
FreeSpaceInPagingFiles
1550460

C:\>wmic os get FreeVirtualMemory
FreeVirtualMemory
2056820

C:\>wmic os get InstallDate
InstallDate
20071022105248.000000-240

C:\>wmic os get LastBootUpTime
LastBootUpTime
20110512100408.221878-240

C:\>wmic os get LocalDateTime
LocalDateTime
20110512105612.789000-240

C:\>wmic os get Locale
Locale
0409

C:\>wmic os get Manufacturer
Manufacturer
Microsoft Corporation

C:\>wmic os get MaxNumberOfProcesses
MaxNumberOfProcesses
-1

C:\>wmic os get MaxProcessMemorySize
MaxProcessMemorySize
2097024

C:\>wmic os get Name
Name
Microsoft Windows XP Professional|C:\WINDOWS|\Device\Harddisk0\Partition1

C:\>wmic os get NumberOfLicensedUsers
NumberOfLicensedUsers

C:\>wmic os get NumberOfProcesses
NumberOfProcesses
43

C:\>wmic os get NumberOfUsers
NumberOfUsers
2

C:\>wmic os get OSLanguage
OSLanguage
1033

C:\>wmic os get OSProductSuite
OSProductSuite

C:\>wmic os get OSType
OSType
18

C:\>wmic os get Organization
Organization

C:\>wmic os get OtherTypeDescription
OtherTypeDescription

C:\>wmic os get PlusProductID
PlusProductID

C:\>wmic os get PlusVersionNumber
PlusVersionNumber

C:\>wmic os get Primary
Primary
TRUE

C:\>wmic os get QuantumLength
QuantumLength
0

C:\>wmic os get QuantumType
QuantumType
0

C:\>wmic os get RegisteredUser
RegisteredUser
<Name Removed>

C:\>wmic os get SerialNumber
SerialNumber
<Serial Number Removed>

C:\>wmic os get ServicePackMajorVersion
ServicePackMajorVersion
3

C:\>wmic os get ServicePackMinorVersion
ServicePackMinorVersion
0

C:\>wmic os get SizeStoredInPagingFiles
SizeStoredInPagingFiles
2523516

C:\>wmic os get Status
Status
OK

C:\>wmic os get SystemDevice
SystemDevice
\Device\HarddiskVolume1

C:\>wmic os get SystemDirectory
SystemDirectory
C:\WINDOWS\system32

C:\>wmic os get SystemDrive
SystemDrive
C:

C:\>wmic os get TotalSwapSpaceSize
TotalSwapSpaceSize

C:\>wmic os get TotalVirtualMemorySize
TotalVirtualMemorySize
2097024

C:\>wmic os get TotalVisibleMemorySize
TotalVisibleMemorySize
1048096

C:\>wmic os get Version
Version
5.1.2600

C:\>wmic os get WindowsDirectory
WindowsDirectory
C:\WINDOWS


----------



## Squashman (Apr 4, 2003)

As far as I had read the other day, that variable should only work in Vista and above based on the fact that 2008 server does share the same code base as Vista but I could be wrong.


----------



## scrfix (May 3, 2009)

Okay. How about if we approach this a different way? I have not checked XP 64 for this yet however every other 64 bit OS I have worked with has a program files directory called "Program Files (x86)". If it is a 32 Bit OS then that does not exist.

If I remember correctly I believe there is even an environmental variable or a registry entry that we can pull this from to verify wehther or not it is on the computer so we do not have to look for it in the C Drive. This will further help verification in case someone decides to be smart and manually make a Program Files (x86) directory on their own 32 bit OS.

How about if we do this in steps.

Step 1: I read that basic versions of the OS do not have WMIC installed. I also saw a script where it is pretty easy to test for WMIC. So let's start out by testing for WMIC:


```
WMIC /? >nul 2>&1
if %errorlevel% EQU 9009 (
    GOTO _CHECKX86
) ELSE (
    GOTO _CHECKOSTYPE
)
```
Step 2: If we did not find WMIC then we simply go check to see if the Program Files x86 folder is there. We can check that one of two ways. We can either check that with the environmental variable %ProgramFiles(x86)% or by checking to see if the registry entry exists which would be HKLM/Software/Microsoft/Windows/CurrentVersion /v "ProgramFilesDir (x86)". If we found WMIC then we go and check what OS we are running which is pretty simple.


```
:: We are on an operating system that does not support wmic os get osarchitecture so we cannot determine whether we are on a 32 or 64 Bit OS so now we check for the Program (x86) directory as a fallback.
:_CHECKX86
IF NOT "%PROGRAMFILES(X86)%" == "" (
Echo "%PROGRAMFILES(X86)%"
Echo I am presumably a 64 Bit OS
SET _something = something
) ELSE (
Echo I am not a 64 Bit OS
SET _something = something
)
 
GOTO _SOMEWHERE_ELSE
```
Step 3: If we found WMIC then we now check for the OS Type. The command for that is *wmic os get name*

```
:_CHECKOSTYPE
FOR /F "tokens=3 delims= " %%a in ('wmic os get name') Do If /i %%a==XP (GOTO _CHECKX86) ELSE (GOTO _CHECKFORSERVER)
```
The above works if you are just checking for XP however the server token is #4. That is below


```
:_CHECKFORSERVER
FOR /F "tokens=4 delims= " %%a in ('wmic os get name') Do If /i %%a==2003 (GOTO _CHECKX86) ELSE (GOTO _CHECK32)
```
Step 4: We finish it up with my code from above. If we got this far then we are on an OS that can handle this code we will definitely be able to tell whether or not we are on a 64 bit OS.

```
:: _osarch will not equal 32 or 64 depending on what OS Architecture you are on. 
:_CHECK32
For /F "tokens=2 delims==,-" %%a In ('wmic os get osarchitecture /value') Do ( 
set _osarch=%%a
)
:: Do what you want with %_osarch% now.
IF _osarch==32 (
echo I am a 32 Bit OS
) ELSE (
echo I am not a 32 Bit OS
)
 
:: Clear the _osarch variable once you are done with it
Set _osarch=
```
That is a little long winded but it takes care of XP, Server 2003 and 64 Bit OS's.
It can presumably take care of Windows 98, ME & 2000 as well since they will not have a Program Files (x86) directory.

*Here is the script all at once*
*How to determine whether an (OS) operating system is a 32 bit or 64 bit operating system. This should be compatible with Windows XP, Server 2003 (all Versions including SBS), Windows Vista, Windows 7, Home, Basic & Professional Versions, Server 2003, 64 Bit and 32 Bit versions and possibly with Windows 2000, 98, Me & 2000 Server but those platforms have not been tested.)*
It does not require the computer have wmic but makes use of it if it is there.


```
@echo off
:: Let's start out by testing for WMIC:
:_TESTWMIC
WMIC /? >nul 2>&1
if %errorlevel% EQU 9009 (
    GOTO _CHECKX86
) ELSE (
    GOTO _CHECKOSTYPE
)
 
:: If we did not find WMIC then we simply go check to see if the Program Files x86 folder is there. We can check that one of two ways. We can either check that with the environmental variable %ProgramFiles(x86)% or by checking to see if the registry entry exists which would be HKLM/Software/Microsoft/Windows/CurrentVersion /v "ProgramFilesDir (x86)". If we found WMIC then we go and check what OS we are running which is pretty simple.
 
:: We are on an operating system that does not support wmic os get osarchitecture so we cannot determine whether we are on a 32 or 64 Bit OS so now we check for the Program (x86) directory as a fallback.
 
:_CHECKX86
IF NOT "%PROGRAMFILES(X86)%" == "" (
echo I am presumably 64 Bit because I have "%PROGRAMFILES(X86)%"
) ELSE (
echo I am more than likely a 32 bit OS because I do not have "%PROGRAMFILES(X86)%"
)
GOTO _SOMEWHERE_ELSE
 
:: If we found WMIC then we now check for the OS Type. The command for that is wmic os get name
 
:_CHECKOSTYPE
FOR /F "tokens=3 delims= " %%a in ('wmic os get name') Do If /i %%a==XP (GOTO _CHECKX86) ELSE (GOTO _CHECKFORSERVER)
 
:: The above works if you are just checking for XP however the server token is #4. That is below
 
:_CHECKFORSERVER
FOR /F "tokens=4 delims= " %%a in ('wmic os get name') Do If /i %%a==2003 (GOTO _CHECKX86) ELSE (GOTO _CHECK32)
 
:: If we got this far then we are on an OS that can handle this code we will definitely be able to tell whether or not we are on a 64 bit OS.
 
:_CHECK32
For /F "tokens=2 delims==,-" %%a In ('wmic os get osarchitecture /value') Do ( 
set _osarch=%%a
)
 
:: Do what you want with %_osarch% now.
echo I am a %_osarch% bit OS.
 
:: Or utilize an IF statement for it.
:: Do what you want with %_osarch% now.
:: IF _osarch==32 (
:: echo I am a 32 Bit OS
:: ) ELSE (
:: echo I am not a 32 Bit OS
:: )
 
:: Clear the _osarch variable once you are done with it
Set _osarch=
pause
```


----------



## Squashman (Apr 4, 2003)

Why bother coding for 98,ME or 2000 anymore? If businesses are still using those operating systems in a production environment they are just asking for Trouble. Just like the people who post here that are still trying to maintain their legacy DOS applications that will only run in DOS.


----------



## scrfix (May 3, 2009)

I agree. I actually didn't code for it. It merely worked out that way. I don't code for 98 or ME. Here in a few years I will stop coding for XP because XP is just a real PIA to code for. Every other OS has the stuff I am looking to work with natively except for XP. I have to go this huge coding work-a-round because of XP. So 4 lines of code for Vista, 7, 2008, etc... 79 lines of code for XP, 2003 to attempt to do the same exact thing. (ahhhhhhhhhh)


----------

