Skip to main content

How to Get Disk Drive Media Type (SSD or HDD) in a Batch Script

Knowing the type of physical disk you are working with: a traditional spinning Hard Disk Drive (HDD) or a modern Solid-State Drive (SSD)—is crucial for writing intelligent system maintenance and optimization scripts. For example, a standard defragmentation is beneficial for an HDD but harmful to an SSD, which requires a different "TRIM" operation instead.

This guide will teach you the modern and most reliable method for determining the media type of a physical disk by calling a simple PowerShell command from within your batch script. You will learn why this is superior to older methods and how to create a script that can list all physical disks and identify them as either an SSD or HDD.

danger

CRITICAL NOTE: Querying low-level hardware information is a privileged operation. For the most reliable and complete results, your script must be run with full administrator privileges.

The Challenge: No Direct "MediaType" Command in Batch

The native batch environment (cmd.exe) has no built-in command to directly ask a drive, "Are you an SSD or an HDD?" This information is stored in the low-level hardware information that the operating system maintains. To get this data, we must use a tool that can query the system's storage management layer.

The best, simplest, and most reliable method is to use PowerShell. It has a modern cmdlet, Get-PhysicalDisk, designed for exactly this purpose.

The PowerShell Command: Get-PhysicalDisk | Select-Object DeviceID,MediaType

  • Get-PhysicalDisk: This cmdlet retrieves a list of all physical disk objects.
  • Select-Object DeviceID,MediaType: This selects just the properties we are interested in. The MediaType property will return a clear string: SSD or HDD.

The Legacy Alternative: Using WMIC (and its limitations)

The WMIC utility can also query this information, but it is often less reliable. WMIC DISKDRIVE GET Index,Model,MediaType

Why this is not recommended:

  • Often Unreliable: On many systems, the MediaType property in WMIC will return a generic value like "Fixed hard disk media" for both SSDs and HDDs, making it useless for differentiation.
  • Less Direct: Another method is to check the RotationRate property (WMIC DISKDRIVE GET RotationRate), where a value of 0 implies an SSD (as it has no moving parts). This is an inference, not a direct statement, and can be inaccurate with some modern storage devices.

For these reasons, the PowerShell method is overwhelmingly superior.

The Script: Listing All Disks and Their Media Types

This script uses the recommended PowerShell one-liner inside a FOR /F loop to get a clean list of all physical disks and their types.

@ECHO OFF
SETLOCAL
ECHO --- System Disk Media Type Report ---
ECHO.
ECHO Querying physical disks...
ECHO.

ECHO Disk Index | Media Type | Model
ECHO ========== | ========== | ==========================

REM The FOR /F loop processes the custom output from the PowerShell command.
FOR /F "tokens=*" %%A IN (
'powershell -NoProfile -ExecutionPolicy Bypass -Command "Get-PhysicalDisk | ForEach-Object { $_.DeviceID, $_.MediaType, $_.FriendlyName -join '|' }"'
) DO (
FOR /F "tokens=1,2,* delims=|" %%I IN ("%%A") DO (
ECHO %%I | %%J | %%K
)
)

ENDLOCAL

How the script works:

  • powershell -Command "...": This executes the PowerShell code from our batch script.
  • Get-PhysicalDisk: Gets all the physical disk objects.
  • ForEach-Object { ... }: This loop runs once for each disk found.
  • $_.DeviceID, $_.MediaType, $_.FriendlyName -join '|': This is the clever part. For each disk, it creates a single string, joining the disk's ID, its Media Type, and its friendly name with a pipe (|) character. This creates a simple, parsable line like 0|SSD|Samsung SSD 970 EVO 500GB.
  • FOR /F "tokens=1,2,* delims=|": The batch script's FOR loop then processes this |-delimited string.
    • delims=|: Sets the delimiter to the pipe character.
    • tokens=1,2,*: Assigns the first part (0) to %%I, the second (SSD) to %%J, and the rest of the line (the model name) to %%K.

Common Pitfalls and How to Solve Them

  • Administrator Rights: To get accurate hardware information, you must run the script as an Administrator. A standard user may not have the rights to query the storage subsystem.

  • RAID Controllers: If your disks are behind a hardware RAID controller, Windows may not be able to see the individual physical disks. The RAID controller often presents the entire array as a single, virtual "disk." In this case, the Get-PhysicalDisk command might report the MediaType as "Unspecified" or show information about the RAID controller itself.

  • PowerShell Execution Policy: On a highly restricted system, the PowerShell command could be blocked. Solution: As shown in the script, using the -ExecutionPolicy Bypass switch is a robust way to ensure your command runs.

Practical Example: A "Smart Defrag" Checker

This script builds on the core logic to create a tool that advises the correct maintenance action for each drive. This is the most common practical application for this check.

@ECHO OFF
SETLOCAL
ECHO --- Smart Drive Maintenance Advisor ---
ECHO.

FOR /F "tokens=*" %%L IN (
'powershell -NoProfile -ExecutionPolicy Bypass -Command "Get-PhysicalDisk | ForEach-Object { $_.DeviceID, $_.MediaType, $_.FriendlyName -join '|' }"'
) DO (
FOR /F "tokens=1,2,* delims=|" %%I IN ("%%L") DO (
ECHO Disk [%%I] - Model: %%K
IF /I "%%J"=="SSD" (
ECHO -> Type: SSD. Recommended command: defrag /O
) ELSE IF /I "%%J"=="HDD" (
ECHO -> Type: HDD. Recommended command: defrag /D
) ELSE (
ECHO -> Type: Unknown. Manual check recommended.
)
ECHO.
)
)

ENDLOCAL

Conclusion

Determining whether a drive is an SSD or HDD is a crucial task for modern system maintenance scripts, and PowerShell provides the definitive tool for it.

  • The core command is powershell -Command "Get-PhysicalDisk".
  • The MediaType property will reliably return SSD or HDD.
  • Avoid legacy WMIC methods, which are often unreliable for this specific task.
  • Always run your script as an Administrator to ensure access to hardware information.

By using this PowerShell one-liner, you can create "hardware-aware" batch scripts that apply the correct, optimized actions for different types of storage devices.