Search code examples
powershellwmidisk

Powershell and wmi, how to map logical disk/volumes to a hard disk or vice versa?


Get-WmiObject -ComputerName $ip -Credential $credential -Class Win32_logicaldisk

This gets me disks as I see them in "My computer", eg. C:, D:, E: Now how I get corresponding underlying physical disks ?

If I run following command

Get-WmiObject -ComputerName $ip -Credential $credential -Class win32_diskdrive

I get disk 0, disk 1, disk 2

So how to find out which logical disk is on which physical disk ?

Another question is how to find out volume number ? If I run diskpart and executes "list volume" I get the following output

  Volume ###  Ltr  Label        Fs     Type        Size     Status     Info
  ----------  ---  -----------  -----  ----------  -------  ---------  --------
  Volume 2     C                NTFS   Partition     59 GB  Healthy    Boot
  ...

How can I find out that logical disk C: is Volume 2 ?

best regards, Primoz.


Solution

  • Try this

    Get-WMIObject Win32_LogicalDisk | Foreach-Object {
        Get-WmiObject -Query "Associators of {Win32_LogicalDisk.DeviceID='$($_.DeviceID)'} WHERE ResultRole=Antecedent"
    } | Format-Table
    

    This gives you the related instances of WIn32_logicalDisk where Win32_LogicalDisk is the dependent entity in the relationship. So, you get the Win32_DiskDrive instances.