none
Get main HDD's serial RRS feed

  • Question

  • Hello

    Hello i need to get the serial number of the main hard drive that have the OS system (not the volume) and  also exclude the other drives and removable drive also.

    also i'm not sure about the difference between the commands "Win32_DiskDrive" and "Win32_PhysicalMedia

    my code is for now

            Dim HDS As String

    Dim hdd As New ManagementObjectSearcher("select * from Win32_DiskDrive") For Each hd In hdd.Get HDS = hd("SerialNumber") Next

    Thanks you

    Monday, October 21, 2019 12:39 PM

Answers

  • You can do something like :

            Dim sPathRoot As String = Path.GetPathRoot(Environment.SystemDirectory)
            Dim sSerialNumber As String = Nothing
    
            For Each drive As ManagementObject In (New ManagementObjectSearcher("select * from Win32_DiskDrive").Get)
                Dim partitionCollection As ManagementObjectCollection = (New ManagementObjectSearcher(String.Format(("associators of {{Win32_DiskDrive.DeviceID='{0}'}} " + "where AssocClass = Win32_DiskDriveToDiskPartition"), drive("DeviceID"))).Get)
                For Each partition As ManagementObject In partitionCollection
                    If (Not (partition) Is Nothing) Then
                        Dim logicalCollection As ManagementObjectCollection = (New ManagementObjectSearcher(String.Format(("associators of {{Win32_DiskPartition.DeviceID='{0}'}} " + "where AssocClass= Win32_LogicalDiskToPartition"), partition("DeviceID"))).Get)
                        For Each logical As ManagementObject In logicalCollection
                            If (Not (logical) Is Nothing) Then
                                Dim volumeEnumerator As ManagementObjectCollection.ManagementObjectEnumerator = (New ManagementObjectSearcher(String.Format(("select * from Win32_LogicalDisk " + "where Name='{0}'"), logical("Name"))).Get.GetEnumerator)
                                volumeEnumerator.MoveNext()
                                Dim volume As ManagementObject = CType(volumeEnumerator.Current, ManagementObject)
                                Dim sDriveLetter As String = volume("DeviceID").ToString() + "\"
                                If (sDriveLetter = sPathRoot) Then
                                    sSerialNumber = drive("SerialNumber").ToString()
                                    Exit For
                                End If
                            End If
                        Next
                    End If
                Next
            Next

    • Marked as answer by AbanoubZak Tuesday, October 22, 2019 4:31 PM
    Monday, October 21, 2019 4:57 PM
  • Thank you for your help i will try this one, but can you please tell me why did you choose to use "Win32_DiskDrive" and not "Win32_PhysicalMedia"?

    It is to link with Win32_DiskDriveToDiskPartition

    If I do "select * from Win32_DiskDriveToDiskPartition", I get :

    Antecedent: \\DESKTOP-EOPIFM5\root\cimv2:Win32_DiskDrive.DeviceID="\\\\.\\PHYSICALDRIVE0"
    Dependent: \\DESKTOP-EOPIFM5\root\cimv2:Win32_DiskPartition.DeviceID="Disk #0, Partition #0"

    • Marked as answer by AbanoubZak Wednesday, October 23, 2019 9:53 AM
    Tuesday, October 22, 2019 3:44 PM

All replies

  • You can do something like :

            Dim sPathRoot As String = Path.GetPathRoot(Environment.SystemDirectory)
            Dim sSerialNumber As String = Nothing
    
            For Each drive As ManagementObject In (New ManagementObjectSearcher("select * from Win32_DiskDrive").Get)
                Dim partitionCollection As ManagementObjectCollection = (New ManagementObjectSearcher(String.Format(("associators of {{Win32_DiskDrive.DeviceID='{0}'}} " + "where AssocClass = Win32_DiskDriveToDiskPartition"), drive("DeviceID"))).Get)
                For Each partition As ManagementObject In partitionCollection
                    If (Not (partition) Is Nothing) Then
                        Dim logicalCollection As ManagementObjectCollection = (New ManagementObjectSearcher(String.Format(("associators of {{Win32_DiskPartition.DeviceID='{0}'}} " + "where AssocClass= Win32_LogicalDiskToPartition"), partition("DeviceID"))).Get)
                        For Each logical As ManagementObject In logicalCollection
                            If (Not (logical) Is Nothing) Then
                                Dim volumeEnumerator As ManagementObjectCollection.ManagementObjectEnumerator = (New ManagementObjectSearcher(String.Format(("select * from Win32_LogicalDisk " + "where Name='{0}'"), logical("Name"))).Get.GetEnumerator)
                                volumeEnumerator.MoveNext()
                                Dim volume As ManagementObject = CType(volumeEnumerator.Current, ManagementObject)
                                Dim sDriveLetter As String = volume("DeviceID").ToString() + "\"
                                If (sDriveLetter = sPathRoot) Then
                                    sSerialNumber = drive("SerialNumber").ToString()
                                    Exit For
                                End If
                            End If
                        Next
                    End If
                Next
            Next

    • Marked as answer by AbanoubZak Tuesday, October 22, 2019 4:31 PM
    Monday, October 21, 2019 4:57 PM
  • You can do something like :

            Dim sPathRoot As String = Path.GetPathRoot(Environment.SystemDirectory)
            Dim sSerialNumber As String = Nothing
    
            For Each drive As ManagementObject In (New ManagementObjectSearcher("select * from Win32_DiskDrive").Get)
                Dim partitionCollection As ManagementObjectCollection = (New ManagementObjectSearcher(String.Format(("associators of {{Win32_DiskDrive.DeviceID='{0}'}} " + "where AssocClass = Win32_DiskDriveToDiskPartition"), drive("DeviceID"))).Get)
                For Each partition As ManagementObject In partitionCollection
                    If (Not (partition) Is Nothing) Then
                        Dim logicalCollection As ManagementObjectCollection = (New ManagementObjectSearcher(String.Format(("associators of {{Win32_DiskPartition.DeviceID='{0}'}} " + "where AssocClass= Win32_LogicalDiskToPartition"), partition("DeviceID"))).Get)
                        For Each logical As ManagementObject In logicalCollection
                            If (Not (logical) Is Nothing) Then
                                Dim volumeEnumerator As ManagementObjectCollection.ManagementObjectEnumerator = (New ManagementObjectSearcher(String.Format(("select * from Win32_LogicalDisk " + "where Name='{0}'"), logical("Name"))).Get.GetEnumerator)
                                volumeEnumerator.MoveNext()
                                Dim volume As ManagementObject = CType(volumeEnumerator.Current, ManagementObject)
                                Dim sDriveLetter As String = volume("DeviceID").ToString() + "\"
                                If (sDriveLetter = sPathRoot) Then
                                    sSerialNumber = drive("SerialNumber").ToString()
                                    Exit For
                                End If
                            End If
                        Next
                    End If
                Next
            Next

    Thank you for your help i will try this one, but can you please tell me why did you choose to use "Win32_DiskDrive" and not "Win32_PhysicalMedia"?
    Tuesday, October 22, 2019 2:58 PM
  • Thank you for your help i will try this one, but can you please tell me why did you choose to use "Win32_DiskDrive" and not "Win32_PhysicalMedia"?

    It is to link with Win32_DiskDriveToDiskPartition

    If I do "select * from Win32_DiskDriveToDiskPartition", I get :

    Antecedent: \\DESKTOP-EOPIFM5\root\cimv2:Win32_DiskDrive.DeviceID="\\\\.\\PHYSICALDRIVE0"
    Dependent: \\DESKTOP-EOPIFM5\root\cimv2:Win32_DiskPartition.DeviceID="Disk #0, Partition #0"

    • Marked as answer by AbanoubZak Wednesday, October 23, 2019 9:53 AM
    Tuesday, October 22, 2019 3:44 PM
  • Thank you it works just fine when i have USB flash drive plugged.
    Tuesday, October 22, 2019 4:32 PM