2

I have few exactly the same drives in my desktop machine. I want to replace one of the drives which contains single volume (and it's the only disk that is used to create the volume). The problem is that I don't want to take out every disk one by one to see if the one removed is the one. Therefore I want to get serial number of the disk.

I'm able to get serial numbers of all disks with wmic and I'm able to get Volume Serial Number of the volume. How can I link them (preferably both using GUI and console)?

3

It is certainly possible to map partitions (and thus drive letters) to the devices in WMI (which wmic queries). However it can require a little effort following the associations from one WMI object to others with WQL: the "SQL Like" query language of WMI (see link for the documentation).

Using PowerShell (because I know it better, and it makes executing expressions much easier):

PSH also has a transcription ability, so with some editing and annotations:

PS [64] E:\ #35> gwmi WIN32_LogicalDisk

[...]

DeviceID     : F:
DriveType    : 3
ProviderName : 
FreeSpace    : 1660612608
Size         : 2147479552
VolumeName   : Messages

[...]

One instance of Win32_LogicalDisk exists per partition: usually a good starting point for anything to do with disks. But I want more details, including the meta-data to uniquely identify this instance so I can look for associations. So use Format-List (aliased to fl) to list all the properties:

PS [64] E:\ #37> gwmi -query 'select * from WIN32_LogicalDisk where DeviceId="F:"' | fl *


PSComputerName               : MyComputer
Status                       : 
Availability                 : 
DeviceID                     : F:
StatusInfo                   : 
__GENUS                      : 2
__CLASS                      : Win32_LogicalDisk
__SUPERCLASS                 : CIM_LogicalDisk
__DYNASTY                    : CIM_ManagedSystemElement
__RELPATH                    : Win32_LogicalDisk.DeviceID="F:"
__PROPERTY_COUNT             : 40
__DERIVATION                 : {CIM_LogicalDisk, CIM_StorageExtent, CIM_LogicalDevice, CIM_LogicalElement...}
__SERVER                     : MyComputer
__NAMESPACE                  : root\cimv2
__PATH                       : \\MyComputer\root\cimv2:Win32_LogicalDisk.DeviceID="F:"
[...]
Caption                      : F:
Compressed                   : False
[...]
Description                  : Local Fixed Disk
DriveType                    : 3
[...]
FileSystem                   : NTFS
FreeSpace                    : 1660612608
[...]
MediaType                    : 12
Name                         : F:
[...]
VolumeName                   : Messages
VolumeSerialNumber           : 5A5189FB
[...]

And then to capture the __RELPATH (__PATH also works, but everything is local here, I don't need the computer name or WMI namespace: everything I'm doing with be in root\cimv2).

PS [64] E:\ #38> $DriveFPath = (gwmi -query 'select * from WIN32_LogicalDisk where DeviceId="F:"').__RELPATH

Now the clever bit: what other objects are linked to the drive's object:

PS [64] E:\ #39> gwmi -query "associators of {$DriveFPath}" | ft -auto __CLASS,__RELPATH

__CLASS              __RELPATH                                           
-------              ---------                                           
Win32_DiskPartition  Win32_DiskPartition.DeviceID="Disk #0, Partition #0"
Win32_Directory      Win32_Directory.Name="f:\\"                         
Win32_ComputerSystem Win32_ComputerSystem.Name="MyComputer"                  

This one looks interesting:

PS [64] E:\ #42> gwmi -query "associators of {$DriveFPath} where resultclass=Win32_diskpartition" | fl *

[...]
Name                        : Disk #0, Partition #0
__GENUS                     : 2
__CLASS                     : Win32_DiskPartition
__SUPERCLASS                : CIM_DiskPartition
__DYNASTY                   : CIM_ManagedSystemElement
__RELPATH                   : Win32_DiskPartition.DeviceID="Disk #0, Partition #0"
__PROPERTY_COUNT            : 34
__DERIVATION                : {CIM_DiskPartition, CIM_StorageExtent, CIM_LogicalDevice, CIM_LogicalElement...}
__SERVER                    : MyComputer
__NAMESPACE                 : root\cimv2
__PATH                      : \\MyComputer\root\cimv2:Win32_DiskPartition.DeviceID="Disk #0, Partition #0"
[...]
Caption                     : Disk #0, Partition #0
[...]
Description                 : GPT: Basic Data
DeviceID                    : Disk #0, Partition #0
[...]

But does not itself contain what I want. So repeat the process:

PS [64] E:\ #43> $DiskPartitionPath = (gwmi -query "associators of {$DriveFPath} where resultclass=Win32_diskpartition").__RELPATH

PS [64] E:\ #47> gwmi -query "associators of {$DiskPartitionPath}" | ft -auto __CLASS,__RELPATH

__CLASS              __RELPATH                                       
-------              ---------                                       
Win32_ComputerSystem Win32_ComputerSystem.Name="MyComputer"              
Win32_DiskDrive      Win32_DiskDrive.DeviceID="\\\\.\\PHYSICALDRIVE0"
Win32_LogicalDisk    Win32_LogicalDisk.DeviceID="F:"                 

So it links back to the Win32_LogicalDisk where we started, but in this case that Win32_DiskDrive looks like it might be helpful (the value of the __RELPATH is a clue: I know that's the kind of naming Windows uses for its physical disks):

PS [64] E:\ #48> gwmi -query "associators of {$DiskPartitionPath} where resultclass=Win32_DiskDrive" | fl *


[...]
DeviceID                    : \\.\PHYSICALDRIVE0
StatusInfo                  : 
Partitions                  : 2
BytesPerSector              : 512
ConfigManagerUserConfig     : False
DefaultBlockSize            : 
Index                       : 0
InstallDate                 : 
InterfaceType               : IDE
[...]
SectorsPerTrack             : 63
Size                        : 1500299297280
TotalCylinders              : 182401
TotalHeads                  : 255
TotalSectors                : 2930272065
TotalTracks                 : 46512255
TracksPerCylinder           : 255
__GENUS                     : 2
__CLASS                     : Win32_DiskDrive
__SUPERCLASS                : CIM_DiskDrive
__DYNASTY                   : CIM_ManagedSystemElement
__RELPATH                   : Win32_DiskDrive.DeviceID="\\\\.\\PHYSICALDRIVE0"
__PROPERTY_COUNT            : 51
__DERIVATION                : {CIM_DiskDrive, CIM_MediaAccessDevice, CIM_LogicalDevice, CIM_LogicalElement...}
__SERVER                    : MyComputer
__NAMESPACE                 : root\cimv2
__PATH                      : \\MyComputer\root\cimv2:Win32_DiskDrive.DeviceID="\\\\.\\PHYSICALDRIVE0"
Availability                : 
Capabilities                : {3, 4}
CapabilityDescriptions      : {Random Access, Supports Writing}
Caption                     : ST31500341AS ATA Device
CompressionMethod           : 
CreationClassName           : Win32_DiskDrive
Description                 : Disk drive
[...]
Model                       : ST31500341AS ATA Device
Name                        : \\.\PHYSICALDRIVE0
PNPDeviceID                 : IDE\DISKST31500341AS____________________________CC1H____\5&2CC8A95A&0&2.0.0
[...]
SerialNumber                : 2020202020202020202020205639325333323235
Signature                   : 
[...]

And there you have it. (I suspect the SerialNumber's value is encoded—20 could be the ASCII code for a space in hexadecimal, but checking that would need opening up my computer and getting the disk drive out: I'll leave that as an exercise :-)).

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.