Combine Get-Disk and LogicalDisk in PowerShell?

I have this query that scans all the data of logical drives:

Write-Host "Drive information for $env:ComputerName" Get-WmiObject -Class Win32_LogicalDisk | Where-Object {$_.DriveType -ne 5} | Sort-Object -Property Name | Select-Object Name, VolumeName, VolumeSerialNumber,SerialNumber, FileSystem, Description, VolumeDirty, ' @{"Label"="DiskSize(GB)";"Expression"={"{0:N}" -f ($_.Size/1GB) -as [float]}}, ' @{"Label"="FreeSpace(GB)";"Expression"={"{0:N}" -f ($_.FreeSpace/1GB) -as [float]}}, ' @{"Label"="%Free";"Expression"={"{0:N}" -f ($_.FreeSpace/$_.Size*100) -as [float]}} | Format-Table -AutoSize 

Conclusion :

enter image description here

However, I need information about physical disks and their partitions / volumes:

So - for physical disks, I have this command:

Get-Disk

Result :

enter image description here

Question:

I want to combine these two teams. I want to see a disk, and under each disk - information about its logical disk:

  • Disk No. 1: .... (information)
    > Information about logical drives .....
  • Disk No. 2: .... (information)
    > This is logical disk information .....
  • Disk No. 3: .... (information)
    > This is logical disk information .....
  • etc...

How can I combine these two queries?

+9
source share
3 answers

You need to request several WMI classes to get all the necessary information.

Partitions can be mapped to their disks using the Win32_DiskDriveToDiskPartition class, and disks can be mapped to their partitions using the Win32_LogicalDiskToPartition class.

 Get-WmiObject Win32_DiskDrive | ForEach-Object { $disk = $_ $partitions = "ASSOCIATORS OF " + "{Win32_DiskDrive.DeviceID='$($disk.DeviceID)'} " + "WHERE AssocClass = Win32_DiskDriveToDiskPartition" Get-WmiObject -Query $partitions | ForEach-Object { $partition = $_ $drives = "ASSOCIATORS OF " + "{Win32_DiskPartition.DeviceID='$($partition.DeviceID)'} " + "WHERE AssocClass = Win32_LogicalDiskToPartition" Get-WmiObject -Query $drives | ForEach-Object { New-Object -Type PSCustomObject -Property @{ Disk = $disk.DeviceID DiskSize = $disk.Size DiskModel = $disk.Model Partition = $partition.Name RawSize = $partition.Size DriveLetter = $_.DeviceID VolumeName = $_.VolumeName Size = $_.Size FreeSpace = $_.FreeSpace } } } } 
+24
source

How about this ...

 Get-CimInstance Win32_Diskdrive -PipelineVariable disk | Get-CimAssociatedInstance -ResultClassName Win32_DiskPartition -PipelineVariable partition | Get-CimAssociatedInstance -ResultClassName Win32_LogicalDisk | Select-Object @{n='Disk';e={$disk.deviceid}}, @{n='DiskSize';e={$disk.size}}, @{n='DiskModel';e={$disk.model}}, @{n='Partition';e={$partition.name}}, @{n='RawSize';e={$partition.size}}, @{n='DriveLetter';e={$_.DeviceID}}, VolumeName,Size,FreeSpace 

Output:

 Disk : \\.\PHYSICALDRIVE0 DiskSize : 128034708480 DiskModel : SAMSUNG MZ7PC128HAFU-000L5 Partition : Disk #0, Partition #0 RawSize : 128034595328 DriveLetter : C: VolumeName : DISK Size : 128034594816 FreeSpace : 7023042560 
+2
source

I did it like this:

[String] ([Math] :: Round ($ _. Size / 1GB, 2)) + 'GB'

-2
source

All Articles