Skip to main content

How to Get the BIOS Serial Number in Batch Script

The BIOS or UEFI serial number is a unique identifier assigned to your computer's motherboard by the manufacturer. It serves as a hardware-level "asset tag" that is independent of the operating system or computer name. Retrieving this serial number is a common task in enterprise environments for asset tracking, inventory management, and software licensing.

This guide will teach you how to use the powerful, built-in WMIC (Windows Management Instrumentation Command-line) utility to query the BIOS for this serial number directly from a batch script. You will learn how to capture this value into a variable and how to use it to identify a specific machine.

The Core Command: WMIC BIOS GET SerialNumber

Windows has no simple environment variable like %SERIALNUMBER%. To get this hardware-level information, you must query the system's management layer. The WMIC utility is the standard command-line interface for this.

The specific command to get the BIOS serial number is: WMIC BIOS GET SerialNumber

  • WMIC: The command-line utility.
  • BIOS: The WMI "alias" for the Win32_BIOS class, which contains information about the system's BIOS/UEFI.
  • GET SerialNumber: The specific property we want to retrieve.

Basic Example: Displaying the Serial Number

Running the command directly in a command prompt is the simplest way to see the information for the local machine.

@ECHO OFF
ECHO --- Querying BIOS Serial Number ---
ECHO.
WMIC BIOS GET SerialNumber

The output from WMIC is formatted with a header and the value, often with extra trailing spaces or lines.

--- Querying BIOS Serial Number ---

SerialNumber
PC0EEA4A

How to Capture the Serial Number in a Variable

To use the serial number in a script (e.g., to save it to a file or compare it), you need to capture it into a variable. The standard method for parsing WMIC output is a FOR /F loop.

@ECHO OFF
SET "BIOS_SERIAL="

ECHO --- Capturing BIOS Serial Number ---

REM 'skip=1' ignores the "SerialNumber" header line.
FOR /F "skip=1" %%S IN ('WMIC BIOS GET SerialNumber') DO (
SET "BIOS_SERIAL=%%S"
GOTO :SerialCaptured
)

:SerialCaptured
IF NOT DEFINED BIOS_SERIAL (
ECHO [FAILURE] Could not retrieve the serial number.
GOTO :EOF
)

ECHO.
ECHO The captured serial number is: %BIOS_SERIAL%
  • skip=1: This tells the FOR loop to ignore the first line of the command's output, which is the "SerialNumber" header.
  • GOTO :SerialCaptured: This is important. It makes the loop exit immediately after processing the first line of data, preventing the BIOS_SERIAL variable from being overwritten by any subsequent blank lines in the WMIC output.

How the Method Works

The WMIC command interfaces with the Windows Management Instrumentation service, which is a core part of the operating system. WMI communicates with the system's hardware drivers and firmware (the BIOS or UEFI) to read standardized information. The SerialNumber property is a standard field defined in the hardware specifications, and WMIC simply provides a way to read and display it.

Common Pitfalls and How to Solve Them

Problem: The Serial Number is Generic (e.g., "To be filled by O.E.M.")

This is the most common issue you will encounter. You run the command, and the output is a useless placeholder string like To be filled by O.E.M., 0123456789, or Default string.

This is not a failure of the script. It is a failure of the computer's manufacturer (Original Equipment Manufacturer) to correctly flash a unique serial number into the motherboard's BIOS at the factory. This is very common with custom-built PCs and some budget-friendly or "white-box" computers. Major brands like Dell, HP, and Lenovo are generally very reliable about setting a proper serial number.

Solution: Your script can check for these common placeholder values.

IF /I "%BIOS_SERIAL%"=="To be filled by O.E.M." (
ECHO The serial number is generic and cannot be used for unique identification.
)

Problem: Parsing the WMIC Output

The output from WMIC often contains extra carriage returns and spaces, which can make parsing difficult if you're not careful.

Solution: The FOR /F "skip=1" loop with an immediate GOTO is the most robust and recommended pattern. It reliably isolates the single line of data you need and ignores the rest of the noisy output.

Practical Example: A Remote Asset Information Script

A key feature of WMIC is its ability to query remote computers on your network (assuming you have administrative rights on the target machine). This script gets the computer name, model, and serial number from a remote machine.

@ECHO OFF
SETLOCAL
SET "TARGET_COMPUTER=REMOTE-PC-01"

ECHO --- Gathering Remote System Information ---
ECHO Target: %TARGET_COMPUTER%
ECHO.

REM The /NODE switch specifies the remote computer to query.
FOR /F "skip=1" %%S IN ('WMIC /NODE:"%TARGET_COMPUTER%" BIOS GET SerialNumber') DO SET "Serial=%%S"
FOR /F "skip=1" %%M IN ('WMIC /NODE:"%TARGET_COMPUTER%" COMPUTERSYSTEM GET Model') DO SET "Model=%%M"

IF NOT DEFINED Serial (
ECHO [FAILURE] Could not connect to the remote machine or retrieve data.
GOTO :End
)

ECHO Computer Model: %Model%
ECHO Serial Number: %Serial%

:End
ENDLOCAL

Conclusion

The WMIC BIOS GET SerialNumber command is the standard, built-in, and most reliable method for retrieving a computer's unique hardware serial number from a batch script.

Key takeaways:

  • Use WMIC BIOS GET SerialNumber to query the local machine.
  • Use a FOR /F "skip=1" loop with a GOTO to capture the clean serial number into a variable.
  • Be prepared for some machines to return a generic, non-unique serial number. This is a manufacturer issue, not a script error.
  • Leverage the /NODE:"hostname" switch to query remote computers, making this an invaluable tool for system inventory.