Windows – How to map which volume resides on which partition in Windows

disk-volumepartitionwindowswmi

Problem: I need to figure out which volume corresponds to what partition(s) which corresponds to what disk in an extremely efficient script able manor. I know how to correspond which partition corresponds to what disk because the disk id is directly in the results of a simple wmic query. However, the first part of the problem is more difficult. How to correlate which volume belongs to which partitions?

Is there a way, using wmic, PowerShell, cmd prompt, to reverse-engineer which volume maps to which partition(s), that works across all currently supported versions of Windows Server (Windows Server 2008R2 – Windows Server 2016)?

If so how would this query look?

Using diskpart to get information is NOT an option. Although it can be used to script out disk operations, it is terrible to use diskpart as a tool to return information about disk configurations. Diskpart output is NOT parsible.

Best Answer

One possible solution, is the following power shell script. Output should be in JSON format. The problem with this solution is that it relies on the get-partition cmdlet. This cmdlet was introduced after windows 2008R2. This would work as a solution if the get-partition cmdlet were to be ported to windows 2008R2, or if I found a solution that would do the same thing on windows 2008R2. Although not a complete solution, it didn't fit as a comment.

$CimPartInfo = get-partition
        "{"
        foreach ($CimPart in $CimPartInfo) {
            if ($CimPart.Guid -eq $null) {
                $PartGUID = [regex]::match($CimPart.AccessPaths, 'Volume({[^}]+})').Groups[1].Value
                }
             else {
                $PartGUID = $CimPart.Guid
                }
            "`"$PartGUID`": {"
            "`"DiskId`": $($CimPart.DiskNumber),"
            "`"PartitionId`": $($CimPart.PartitionNumber),"
            "`"Type`": `"$($CimPart.Type)`","
            "`"Size`": $($CimPart.Size),"
            "`"Offset`": $($CimPart.Offset),"
            "`"GUID`": `"$($CimPart.Guid)`","
            $x = $CimPart.IsBoot
            "`"Bootable`": $($x.ToString().ToLower()),"
            "`"Status`": `"$($CimPart.OperationalStatus)`""
            if ($CimPart -eq $CimPartInfo[-1]){ "}"}
            else {"},"}
            } #foreach CimPart
        "}"