Skip to content

Instantly share code, notes, and snippets.

@kewalaka
Created January 5, 2022 01:13
Show Gist options
  • Save kewalaka/93dac14574701b25931d990601c6acc3 to your computer and use it in GitHub Desktop.
Save kewalaka/93dac14574701b25931d990601c6acc3 to your computer and use it in GitHub Desktop.
Get-WindowsDiskInfo.ps1 - used to enumerate model/manufacturer & space info about disks
$results = @()
foreach ($disk in Get-CimInstance Win32_Diskdrive) {
Write-Host "Fetching info for $($disk.DeviceID)..."
$diskMetadata = Get-Disk | Where-Object { $_.Number -eq $disk.Index } | Select-Object -First 1
# you don't get meta data back from a dynamic disk
if (!$diskMetadata) {
}
$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
if ($diskMetadata) {
$serial = $diskMetadata.SerialNumber.Trim()
$Manufacturer = $diskMetadata.Manufacturer
$Model = $diskMetadata.Model
$PartitionStyle = $diskMetadata.PartitionStyle
}
else {
#could check this using
#Get-WmiObject Win32_DiskPartition -filter "Type='Logical Disk Manager'" | where DiskIndex -eq $disk.Index
$Manufacturer, $PartitionStyle, $serial = 'N/A'
$Model = $disk.Model
}
# partitions are listed multiple times for dynamic disks
if ($results.driveletter -notcontains $drive.DeviceID) {
$results += [PSCustomObject] @{
DriveLetter = $drive.DeviceID
Number = $disk.Index
Label = $volume.FileSystemLabel
Manufacturer = $Manufacturer
Model = $Model
SerialNumber = $serial
Name = $disk.Caption
FileSystem = $volume.FileSystem
PartitionKind = $PartitionStyle
TotalSpace = $totalSpace
FreeSpace = $freeSpace
UsedSpace = $usedSpace
Drive = $drive
Partition = $partition
Disk = $disk
}
}
}
}
}
$results | sort DriveLetter | select DriveLetter, Label, Manufacturer, Model, TotalSpace, FreeSpace, UsedSpace | ft -AutoSize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment