Combine `Get-Disk` info and `LogicalDisk` info in PowerShell?
I have this query which scans all logical disks information :
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
The output is :
However - I'm after the physical disks information and their partitions / volume information :
So - for physical disks I have this command :
Get-Disk
Result :
Question :
I want to combine between those 2 commands . I want to see the Disk , and below each disk - its logical disk information :
- Disk Number 1 : ....(info)
>
Its logical disks info..... - Disk Number 2 : ....(info)
>
It's logical disks info..... - Disk Number 3 : ....(info)
>
It's logical disks info..... - etc...
How can I combine between those 2 queries ?
Solution 1:
You need to query several WMI classes to get all information you want.
-
Win32_DiskDrive
gives you information about the physical disks. -
Win32_DiskPartition
gives you information about the partitions on the physical disks. -
Win32_LogicalDisk
gives you information about the filesystems inside the partitions.
Partitions can be mapped to their disks using the Win32_DiskDriveToDiskPartition
class, and drives can be mapped to their partitions via 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
}
}
}
}
Solution 2:
How about like this...
Get-CimInstance Win32_Diskdrive -PipelineVariable disk |
Get-CimAssociatedInstance -ResultClassName Win32_DiskPartition -pv 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
Unfortunately this doesn't work with more than one drive. Get-cimassociatedinstance blocks like sort-object, and only the latest pipeline variable is set. Here's a workaround:
Get-CimInstance Win32_Diskdrive -PipelineVariable disk |
% { Get-CimAssociatedInstance $_ -ResultClass Win32_DiskPartition -pv 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
Disk : \\.\PHYSICALDRIVE0
DiskSize : 128034708480
DiskModel : SAMSUNG MZ7PC128HAFU-000L5
Partition : Disk #0, Partition #0
RawSize : 128034595328
DriveLetter : C:
VolumeName : DISK
Size : 128034594816
FreeSpace : 4226514944
Disk : \\.\PHYSICALDRIVE1
DiskSize : 7797565440
DiskModel : USB Flash Memory USB Device
Partition : Disk #1, Partition #0
RawSize : 7801405440
DriveLetter : E:
VolumeName : WINPE
Size : 7784628224
FreeSpace : 7222669312
Solution 3:
Inspired js2010's answer, with some enhancements:
For instance, the Manufacturer
field seems to have some a placeholder value when retrieved from the Win32_DiskDrive
instance, but has the proper value when using the Get-Disk
commandlet.
function Get-Drive {
foreach($disk in Get-CimInstance Win32_Diskdrive) {
$diskMetadata = Get-Disk | Where-Object { $_.Number -eq $disk.Index } | Select-Object -First 1
$partitions = Get-CimAssociatedInstance -ResultClassName Win32_DiskPartition -InputObject $disk
foreach($partition in $partitions) {
$drives = Get-CimAssociatedInstance -ResultClassName Win32_LogicalDisk -InputObject $partition
foreach($drive in $drives) {
$totalSpace = [math]::Round($drive.Size / 1GB, 3)
$freeSpace = [math]::Round($drive.FreeSpace / 1GB, 3)
$usedSpace = [math]::Round($totalSpace - $freeSpace, 3)
$volume = Get-Volume |
Where-Object { $_.DriveLetter -eq $drive.DeviceID.Trim(":") } |
Select-Object -First 1
[PSCustomObject] @{
DriveLetter = $drive.DeviceID
Number = $disk.Index
Label = $volume.FileSystemLabel
Manufacturer = $diskMetadata.Manufacturer
Model = $diskMetadata.Model
SerialNumber = $diskMetadata.SerialNumber.Trim()
Name = $disk.Caption
FileSystem = $volume.FileSystem
PartitionKind = $diskMetadata.PartitionStyle
TotalSpace = $totalSpace
FreeSpace = $freeSpace
UsedSpace = $usedSpace
Drive = $drive
Partition = $partition
Disk = $disk
}
}
}
}
}
Example Usages:
Get-Drive | Format-List
Get-Drive | Where-Object { $_.DriveLetter -eq 'G:' }
Output:
DriveLetter : G:
Number : 5
Label : SE-LXY2-298GB
Manufacturer : Seagate
Model : FreeAgent Go
SerialNumber : 2GE45CK1
Name : Seagate FreeAgent Go USB Device
FileSystem : NTFS
PartitionKind : MBR
TotalSpace : 298.089
FreeSpace : 297.865
UsedSpace : 0.224
Drive : Win32_LogicalDisk: G: (DeviceID = "G:")
Partition : Win32_DiskPartition: Disk #5, Partition #0 (DeviceID = "Disk #5, Partition #0")
Disk : Win32_DiskDrive: Seagate FreeAgent Go USB Device (DeviceID = "\\.\PHYSICALDRIVE5")