Created
January 5, 2022 01:13
-
-
Save kewalaka/93dac14574701b25931d990601c6acc3 to your computer and use it in GitHub Desktop.
Get-WindowsDiskInfo.ps1 - used to enumerate model/manufacturer & space info about disks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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