-1

I have to get the serialnumber of the disk on which my operating system is installed.

I know in order to get serial number i need to run:

>wmic diskdrive get serialnumber,capabilities
Capabilities  SerialNumber
{3, 4}        AI92NXXXXXXXX2G02
{3, 4, 7}     1172XXXXXX030

There are no attributes to check if the OS is installed on this disk.

Dinesh Gowda
  • 1,044
  • 3
  • 13
  • 29
  • _check if the OS is installed_ on a particular disk drive? I doubt that it's possible using `wmi`. However, you can check where the running instance of Windows was booted from (`wmic OS get BootDevice,SystemDevice,SystemDirectory`) and further info from `wmic path Win32_BootConfiguration get /value`… – JosefZ Jun 19 '19 at 11:09
  • Bascially i want to find the serial number of the disk on which the OS is installed – Dinesh Gowda Jun 19 '19 at 11:42
  • I tired to `wmic partition where Bootable=True` and then tried to backtrack to disk didn't help – Dinesh Gowda Jun 19 '19 at 11:44
  • I don't see why this question is closed. It is very on-topic. – Aykhan Hagverdili Aug 24 '21 at 18:55

1 Answers1

1

Start using wmic partition where Bootable=True and then backtrack to Win32_DiskDrive (a possible approach):

@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
for /F "delims=" %%G in ('
  wmic path Win32_DiskPartition where "Bootable=True" get DeviceID /Value
') do ( 
  for /F "tokens=1* delims==" %%g in ("%%G") do (
    set "_DiskPartition=%%h"
    REM ECHO set "_DiskPartition=%%h"
    call :GetDiskDriveIdAndOutput
  )
)
echo Possibly no linkage to a logical disk:
2>NUL wmic path Win32_LogicalDisk ^
  ASSOC /RESULTROLE:Antecedent ^
        /ASSOCCLASS:Win32_LogicalDiskToPartition ^
        /RESULTCLASS:Win32_DiskPartition  
ENDLOCAL
goto :eof

:GetDiskDriveIdAndOutput
for /F tokens^=^2^ delims^=^" %%B in ('          
    wmic path Win32_DiskPartition where "Bootable=True" ASSOC /ASSOCCLASS:Win32_DiskDriveToDiskPartition
  ') do (
      if NOT "%%B"=="%_DiskPartition%" (
        REM ECHO set "_DiskDriveId=%%B"
        set "_DiskDriveId=%%B"
      )
)
echo Bootable: Drive = "%_DiskDriveId:\\=\%", Partition = "%_DiskPartition%"
wmic path Win32_DiskDrive get Capabilities,DeviceId,SerialNumber
REM wmic path Win32_DiskDrive Where "DeviceId='%_DiskDriveId%'" get Capabilities,DeviceId,SerialNumber
goto :eof

Of course, it's improvable from actual output

Bootable: Drive = "\\.\PHYSICALDRIVE0", Partition = "Disk #0, Partition #0"
Capabilities  DeviceID            SerialNumber
{3, 4}        \\.\PHYSICALDRIVE0  NXXXXXXXXK4R2DT
{3, 4, 7}     \\.\PHYSICALDRIVE1  S0NFJNXXXXXXXX

to something like

Capabilities  DeviceID            SerialNumber      Bootable
{3, 4}        \\.\PHYSICALDRIVE0  NXXXXXXXXK4R2DT   True
{3, 4, 7}     \\.\PHYSICALDRIVE1  S0NFJNXXXXXXXX  
JosefZ
  • 28,460
  • 5
  • 44
  • 83