Skip to main content

How to Get the Hard Drive Model and Serial Number in Batch Script

For system inventory, asset tracking, or diagnostic purposes, you often need to identify the specific physical storage devices installed in a computer. The drive's model and serial number are unique hardware identifiers that are perfect for this task. This information is stored in the drive's firmware and is not available through simple file system commands.

This guide will teach you how to use the powerful, built-in WMIC (Windows Management Instrumentation Command-line) utility to query the system for the model and serial number of all installed physical disk drives. You will learn how to parse this information and create a useful inventory report.

The Core Command: WMIC DISKDRIVE GET

The standard way to get hardware information from the command line is with WMIC. The specific WMI "alias" for physical storage devices is DISKDRIVE, which corresponds to the Win32_DiskDrive class.

The command to get the model and serial number is: WMIC DISKDRIVE GET Model, SerialNumber

  • WMIC: The command-line utility.
  • DISKDRIVE: The alias for physical disk drives (HDDs, SSDs, NVMe drives, and sometimes USB drives).
  • GET Model, SerialNumber: The specific properties we want to retrieve.

Key Hard Drive Properties Explained

  • Model: A string provided by the manufacturer that identifies the product (e.g., Samsung SSD 860 EVO 500GB).
  • SerialNumber: A unique string assigned to the physical device at the factory. This should be unique for each drive.
  • Index: The physical index of the drive (e.g., 0, 1). This is the "PhysicalDriveX" number.
  • Size: The total size of the drive in bytes.

Basic Example: Displaying Drive Information

You can run this command directly in a command prompt to see the drives installed in your current machine.

@ECHO OFF
ECHO --- Querying Hard Drive Information ---
ECHO.
WMIC DISKDRIVE GET Model, SerialNumber, Index

The output from WMIC is formatted in columns, showing a system with two drives:

--- Querying Hard Drive Information ---

Index Model SerialNumber
0 Samsung SSD 860 EVO 500GB S4DMF0K425111X
1 WDC WD10EZEX-00WN4A0 WD-WCC6Y1EY2EY3

How to Capture the Information in a Script

To use this data, we need to parse the WMIC output. The default table format can be difficult to parse because the model name can contain spaces. The best way to handle this is to ask WMIC to format the output as CSV (Comma-Separated Values).

@ECHO OFF
SETLOCAL
ECHO --- Capturing Drive Information ---
ECHO.

REM 'skip=1' ignores the header line.
REM 'tokens=2,3 delims=,' splits the CSV, grabbing the 2nd (Model) and 3rd (Serial) columns.
FOR /F "skip=1 tokens=2,3 delims=," %%A IN (
'WMIC DISKDRIVE GET Model,SerialNumber /FORMAT:CSV'
) DO (
ECHO Model: "%%A", Serial: "%%B"
)

ENDLOCAL

This method is extremely robust because the comma delimiter reliably separates the fields, regardless of spaces in the model name.

How the WMIC Method Works

The WMIC command is an interface to the WMI service, which manages operating system data. The Win32_DiskDrive class represents a physical disk drive as seen by the BIOS. The information, including the model and serial number, is read directly from the device's firmware, making it an authoritative hardware identifier. The /FORMAT:CSV switch tells WMIC to format its output with a comma between each property, making it perfect for the FOR /F command to parse.

Common Pitfalls and How to Solve Them

Problem: The System Has Multiple Hard Drives

The WMIC command lists all physical drives. The simple FOR /F loop shown in the capture example will correctly iterate through all of them, processing one per line. This is the desired behavior for inventory scripts.

Problem: Parsing the WMIC Output is Difficult

If you don't use the /FORMAT:CSV switch, parsing the default table format is very tricky because the variable number of spaces in the Model column breaks simple token-based parsing.

Solution: Always use /FORMAT:CSV when you need to parse WMIC output in a script. It makes the process predictable and reliable.

Problem: The Serial Number is Missing or Generic

Some storage devices, particularly certain brands of SSDs, NVMe drives, or external USB drives, may not correctly report a unique serial number. You might get a blank value or a generic string.

Solution: This is a limitation of the hardware's firmware, not the script. Your script should be written to handle this possibility, for example by reporting "N/A" if the serial number variable is empty.

Practical Example: A Drive Inventory Report Script

This script gathers key information about all physical drives in a system and saves it to a CSV file on the desktop, a perfect tool for system administrators.

@ECHO OFF
SETLOCAL
SET "REPORT_FILE=%USERPROFILE%\Desktop\Disk_Inventory.csv"

ECHO --- Disk Inventory Script ---
ECHO Creating report at: "%REPORT_FILE%"

REM Create the CSV header
(ECHO "Index","Model","SerialNumber","Size_GB") > "%REPORT_FILE%"

REM Get the data from WMIC in CSV format and append to our report file.
REM 'skip=1' ignores the WMIC header. 'tokens=2-5' grabs the 4 data columns.
FOR /F "skip=1 tokens=2,3,4,5 delims=," %%I IN (
'WMIC DISKDRIVE GET Index,Model,SerialNumber,Size /FORMAT:CSV'
) DO (
SET "Index=%%I"
SET "Model=%%J"
SET "Serial=%%K"
SET "SizeBytes=%%L"

REM Convert bytes to GB inside PowerShell for 64-bit math
FOR /F %%G IN ('powershell -Command "!SizeBytes! / 1GB"') DO SET "SizeGB=%%G"

ECHO "!Index!","!Model!","!Serial!","!SizeGB!" >> "%REPORT_FILE%"
)

ECHO.
ECHO --- Report Complete ---
START "" "%REPORT_FILE%"
ENDLOCAL

Conclusion

The WMIC DISKDRIVE command is the standard, built-in, and most reliable method for retrieving hardware information about physical storage devices.

Key takeaways:

  • Use WMIC DISKDRIVE GET Model,SerialNumber to query for drive information.
  • For scripting, always use the /FORMAT:CSV switch. This makes parsing the output with a FOR /F loop trivial and reliable.
  • Be prepared for some devices not to report a unique serial number; this is a hardware limitation.
  • This command can be extended to query remote machines (/NODE:"hostname") for powerful network-wide inventory.