Last active
August 19, 2025 23:25
-
-
Save grenade/232e5da83cf39a0d507c to your computer and use it in GitHub Desktop.
get disk, partition, drive info in powershell...
This file contains hidden or 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
| function Get-DiskPartitions { | |
| $partitions = Get-WmiObject -Class Win32_DiskDriveToDiskPartition | % { | |
| $dDep = $_.Dependent | |
| $p = Get-WmiObject -Class Win32_DiskPartition | Where-Object { $_.Path.Path -eq $dDep } | |
| $d = Get-WmiObject -Class Win32_LogicalDiskToPartition | Where-Object { $_.Antecedent -in $dDep } | % { | |
| $lDep = $_.Dependent | |
| Get-WmiObject -Class Win32_LogicalDisk | Where-Object { $_.Path.Path -in $lDep } | |
| } | |
| New-Object PSObject -Property @{ | |
| Drive = if ($d -eq $null) { $null } else { $d.DeviceID }; | |
| Partition = [Int]::Parse($p.Name.Split(",")[1].Replace(" Partition #","")); | |
| Disk = [Int]::Parse($p.Name.Split(",")[0].Replace("Disk #","")); | |
| Volume = if ($d -eq $null) { $null } else { $d.VolumeName } | |
| } | |
| } | |
| return ($partitions | Select Disk | Sort-Object -Property Disk -Unique) | % { | |
| $disk = $_.Disk; | |
| New-Object PSObject -Property @{ | |
| Disk = $disk; | |
| Drives = ($partitions | Where-Object { $_.Disk -eq $disk }) | % { | |
| New-Object PSObject -Property @{ | |
| Drive = $_.Drive; | |
| Partition = $_.Partition; | |
| Volume = $_.Volume; | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment