Skip to main content

How to Get the Amount of Physical RAM in a Batch Script

Knowing the amount of installed physical memory (RAM) is a common requirement for diagnostic scripts or for pre-flight checks before a memory-intensive application is installed or run. A script can use this information to verify that a system meets the minimum requirements, preventing installation failures or poor performance.

This guide will teach you the modern, reliable, and recommended method for getting the total physical RAM using the WMIC (Windows Management Instrumentation) command. You will learn the correct query to use, how to capture the value (which is in bytes) into a variable, and how to convert it into a more human-readable format like megabytes or gigabytes.

The Challenge: No Simple %RAM% Variable

Like most hardware details, there is no simple environment variable like %RAM% that a batch script can access. To get this information, the script must query the Windows operating system's hardware management layer. While several tools can do this, WMIC provides the cleanest, most script-friendly output.

The Core Method: WMIC COMPUTERSYSTEM

The WMIC (Windows Management Instrumentation Command-line) utility is the standard tool for querying system information. We can use it to access the Win32_ComputerSystem class, which contains a property that gives us the exact amount of RAM.

Command: WMIC COMPUTERSYSTEM GET TotalPhysicalMemory /VALUE

  • COMPUTERSYSTEM: The WMI "class" that represents the computer system as a whole.
  • GET TotalPhysicalMemory: Specifies the exact property we want to retrieve. This value is given in bytes.
  • /VALUE: Formats the output as a simple Key=Value list, which is the easiest format for a batch script to parse.

The Legacy Alternative (To Avoid): Parsing systeminfo

An older method involves running the systeminfo command and trying to parse its human-readable output.

systeminfo | find "Total Physical Memory"

Why this method is not recommended:

  • Slow: systeminfo gathers a huge amount of data and is much slower than a targeted WMIC query.
  • Brittle: The output text ("Total Physical Memory") can change depending on the Windows display language, breaking the find command.
  • Hard to Parse: The output includes text and commas (e.g., 16,384 MB), which is difficult to convert into a clean number for calculations.

The WMIC method is superior in every way for scripting.

The Script: Getting the Total RAM with WMIC

This script uses the recommended WMIC method to get the total RAM in bytes and stores it in a variable.

@ECHO OFF
SETLOCAL
SET "TotalRAM_Bytes="

ECHO --- Getting Total Physical Memory ---
ECHO.

REM The FOR /F loop processes the Key=Value output from WMIC.
FOR /F "tokens=2 delims==" %%R IN (
'WMIC COMPUTERSYSTEM GET TotalPhysicalMemory /VALUE'
) DO (
SET "TotalRAM_Bytes_Raw=%%R"
)

REM Clean the variable of any invisible trailing characters (a WMIC bug).
FOR %%N IN (%TotalRAM_Bytes_Raw%) DO SET "TotalRAM_Bytes=%%N"

IF NOT DEFINED TotalRAM_Bytes (
ECHO [ERROR] Could not retrieve memory information.
) ELSE (
ECHO Total RAM found: %TotalRAM_Bytes% bytes
)

ENDLOCAL

How the WMIC Script Works

  • FOR /F "tokens=2 delims==" %%R IN ('...'): This is the core parsing logic.
    • 'WMIC ... /VALUE': This command is executed, and its output is captured. The output is TotalPhysicalMemory=17179869184 (followed by some blank lines).
    • tokens=2 delims==: This tells the FOR loop to split each line by the = delimiter. The part after the = (the second token) is stored in %%R.
  • The "Cleaning" Loop: The second FOR %%N IN (...) loop is a standard trick to clean up a value from WMIC, which sometimes includes a stray carriage return character that can break calculations. This reassignment strips any such characters.

Common Pitfalls and How to Solve Them

The biggest pitfall is dealing with the large number that WMIC returns.

  • The Value is in Bytes: The number returned is the total memory in bytes. 17,179,869,184 bytes is 16 GB. This number is not very human-readable.
  • Exceeds SET /A Limit: This byte value is almost always larger than 2,147,483,647, which is the maximum value that the batch SET /A command can handle (the 32-bit signed integer limit). Trying to perform math on this number directly will cause an overflow and give an incorrect result.

Solution: Scale the Number Down To make the number human-readable and usable in calculations, you must divide it.

@ECHO OFF
SETLOCAL
SET "TotalRAM_Bytes=17179869184"

SET /A "TotalRAM_MB = %TotalRAM_Bytes% / 1048576"
SET /A "TotalRAM_GB = %TotalRAM_MB% / 1024"

ECHO Total RAM is approximately %TotalRAM_GB% GB (%TotalRAM_MB% MB).
ENDLOCAL
note

This calculation itself might fail if the byte count is too large. A safer way is to use a more powerful tool for the conversion.

The PowerShell Method for Conversion: For a truly robust conversion, delegating to PowerShell is best.

FOR /F %%G IN ('powershell -Command "%TotalRAM_Bytes% / 1GB"') DO SET "TotalRAM_GB=%%G"
ECHO Total RAM is %TotalRAM_GB% GB.

Practical Example: A Minimum RAM Requirement Check

This script checks if the local system has at least 8 GB of RAM before allowing a process to continue.

@ECHO OFF
SETLOCAL
SET "RequiredGB=8"
SET /A "RequiredBytes_Approx = %RequiredGB% * 1073000000"
SET "TotalRAM_Bytes="

ECHO --- System Memory Pre-Flight Check ---
ECHO Required memory: %RequiredGB% GB
ECHO.

REM --- Get the total RAM in bytes ---
FOR /F "tokens=2 delims==" %%R IN ('WMIC COMPUTERSYSTEM GET TotalPhysicalMemory /VALUE') DO SET "TotalRAM_Bytes_Raw=%%R"
FOR %%N IN (%TotalRAM_Bytes_Raw%) DO SET "TotalRAM_Bytes=%%N"

ECHO Available memory: %TotalRAM_Bytes% bytes
ECHO.

IF %TotalRAM_Bytes% LSS %RequiredBytes_Approx% (
ECHO [FAILURE] System does not meet the minimum RAM requirement.
ECHO Installation aborted.
) ELSE (
ECHO [SUCCESS] Sufficient memory is available.
ECHO Proceeding with installation...
)

ENDLOCAL
note

We use an approximate byte value for the requirement because of the 32-bit math limit.

Conclusion

The WMIC command is the standard and most reliable method for getting the total amount of physical RAM from a batch script.

  • The core command is WMIC COMPUTERSYSTEM GET TotalPhysicalMemory /VALUE.
  • Use a FOR /F loop to parse the Key=Value output and store the result in a variable.
  • Remember that the value is returned in bytes and is a very large number.
  • For calculations, you will need to scale the value down to megabytes or gigabytes, and for maximum reliability, using PowerShell for the conversion is recommended.