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 ?
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 | % {
$disk = $_
$partitions = "ASSOCIATORS OF " +
"{Win32_DiskDrive.DeviceID='$($disk.DeviceID)'} " +
"WHERE AssocClass = Win32_DiskDriveToDiskPartition"
Get-WmiObject -Query $partitions | % {
$partition = $_
$drives = "ASSOCIATORS OF " +
"{Win32_DiskPartition.DeviceID='$($partition.DeviceID)'} " +
"WHERE AssocClass = Win32_LogicalDiskToPartition"
Get-WmiObject -Query $drives | % {
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
}
}
}
}
How about like 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
I did it like this:
[String]([Math]::Round($_.Size/1GB, 2)) + ' GB'