Skip to main content

How to Get the Total Physical Memory (RAM) Capacity in Batch Script

Knowing the total amount of installed physical memory (RAM) is a fundamental part of any system inventory, diagnostic report, or pre-flight check for memory-intensive software. While you can find this in the graphical System Properties window, a command-line method is essential for automation and scripting.

This guide will teach you how to use the built-in WMIC (Windows Management Instrumentation Command-line) utility to quickly and reliably query the system for the total amount of physical RAM. You will learn how to get the value in bytes and how to perform a simple calculation to convert it to a more human-readable format like gigabytes (GB).

Core Command: WMIC COMPUTERSYSTEM GET

The simplest and most direct way to get the total amount of physical memory is to query the Win32_ComputerSystem WMI class. This class represents the computer as a whole and has a property specifically for this purpose.

Syntax: WMIC COMPUTERSYSTEM GET TotalPhysicalMemory

  • WMIC: The command-line utility.
  • COMPUTERSYSTEM: The WMI alias for the Win32_ComputerSystem class.
  • GET TotalPhysicalMemory: The specific property that holds the total RAM size in bytes.

Alternative Method: WMIC MEMPHYSICAL GET

An alternative method is to query the Win32_PhysicalMemory class. This class represents each individual stick of RAM installed in the computer. To get the total, you must retrieve the Capacity of each stick and add them together.

Syntax: WMIC MEMPHYSICAL GET Capacity

While this is useful for seeing the size of each individual RAM module, it is more complex for just getting the total, which makes the COMPUTERSYSTEM method preferable for that specific task.

Basic Example: Displaying the Total Memory in Bytes

You can run this command directly in a command prompt to see the total RAM for your current machine.

@ECHO OFF
ECHO --- Querying Total Physical Memory ---
ECHO.
WMIC COMPUTERSYSTEM GET TotalPhysicalMemory

The output from WMIC is the total memory in bytes, along with a header. This example is from a system with 16 GB of RAM.

--- Querying Total Physical Memory ---

TotalPhysicalMemory
17179869184

How to Capture and Convert the Value in a Script

The number of bytes is not very user-friendly. A good script will capture this value and convert it to a more readable unit, like gigabytes (GB). Since the number of bytes will be very large (well over the 2.1 billion limit of SET /A), we must use a more powerful tool like PowerShell to perform the conversion.

@ECHO OFF
SET "TotalBytes="

ECHO --- Capturing and Converting Total RAM ---

REM --- Step 1: Capture the raw byte value ---
REM 'skip=1' ignores the header line.
FOR /F "skip=1" %%B IN ('WMIC COMPUTERSYSTEM GET TotalPhysicalMemory') DO (
SET "TotalBytes=%%B"
GOTO :BytesCaptured
)

:BytesCaptured
IF NOT DEFINED TotalBytes (
ECHO [FAILURE] Could not retrieve memory information.
GOTO :EOF
)

ECHO.
ECHO Total Memory (Bytes): %TotalBytes%

REM --- Step 2: Convert bytes to gigabytes using PowerShell ---
SET "TotalGB="
FOR /F %%G IN (
'powershell -Command "%TotalBytes% / 1GB"'
) DO (
SET "TotalGB=%%G"
)

ECHO Total Memory (Gigabytes): %TotalGB% GB

How the Methods Work

  • WMIC COMPUTERSYSTEM GET TotalPhysicalMemory: This command queries the Win32_ComputerSystem WMI object and retrieves a single, pre-calculated value that represents the total physical memory as seen by the operating system.
  • PowerShell Conversion: The command powershell -Command "%TotalBytes% / 1GB" works because PowerShell has built-in unit multipliers. 1GB is automatically understood as 1073741824 (2^30 bytes), and PowerShell's 64-bit math engine can handle these large numbers without issue.

Common Pitfalls and How to Solve Them

  • 32-Bit Math Limitation: The primary pitfall is attempting to perform the byte-to-gigabyte conversion using batch's native SET /A command. SET /A is limited to 32-bit integers and will fail with the large numbers involved in modern RAM sizes. Solution: You must use a tool with a 64-bit math engine, like PowerShell, for the conversion.
  • Parsing WMIC Output: As with all WMIC commands, the output has a header and extra blank lines. Solution: The FOR /F "skip=1" loop with an immediate GOTO is the most reliable pattern to capture the single value and ignore the rest of the output.

Practical Example: A "Minimum System Requirements" Check

This script checks if the computer has at least 8 GB of RAM before allowing an installation to proceed.

@ECHO OFF
SETLOCAL
SET "RequiredGB=8"

ECHO --- System Memory Requirement Check ---
ECHO Required RAM: %RequiredGB% GB
ECHO.

SET "TotalBytes="
FOR /F "skip=1" %%B IN ('WMIC COMPUTERSYSTEM GET TotalPhysicalMemory') DO (
SET "TotalBytes=%%B" & GOTO :GotBytes
)
:GotBytes
IF NOT DEFINED TotalBytes (ECHO [ERROR] Could not get memory info. & GOTO :End)

SET "InstalledGB="
FOR /F %%G IN ('powershell -Command "[Math]::Round(%TotalBytes% / 1GB)"') DO (
SET "InstalledGB=%%G"
)

ECHO Installed RAM: %InstalledGB% GB
ECHO.

IF %InstalledGB% GEQ %RequiredGB% (
ECHO [SUCCESS] The system meets the minimum memory requirement.
) ELSE (
ECHO [FAILURE] Not enough memory. Installation requires at least %RequiredGB% GB.
)

:End
ENDLOCAL
note

[Math]::Round(...) is used in PowerShell to round the result to the nearest whole number.

Conclusion

The WMIC command is the definitive, built-in tool for retrieving the total amount of physical RAM from a batch script.

Key takeaways:

  • Use WMIC COMPUTERSYSTEM GET TotalPhysicalMemory to get the total RAM in bytes.
  • Use a FOR /F "skip=1" loop with a GOTO to reliably capture this value into a variable.
  • Because the number of bytes is too large for batch's SET /A command, you must use PowerShell to convert the value to gigabytes (powershell -Command "%bytes% / 1GB").
  • This method can be extended to query remote machines (/NODE:"hostname") for system inventory.