Find Volume number in Powershell

Alex Palmer 21 Reputation points
2020-12-10T08:51:29.09+00:00

I want to link volume number and drive letter on a remote device.

The output from diskpart is like this

Volume ### Ltr Label Fs Type Size Status Info


Volume 0 E DVD-ROM 0 B No Media
Volume 1 System Rese NTFS Partition 350 MB Healthy System
Volume 2 C NTFS Partition 49 GB Healthy Boot
Volume 3 D Data NTFS Partition 99 GB Healthy
Volume 4 L Logs NTFS Partition 49 GB Healthy
I want to retrieve the the Volume## value for a given drive letter. So if I'm interested in drive letter D I want Volume 3 returned.

I tried running diskpart remotely using this
$part = Invoke-Command -Computername $vm -scriptblock {$commands=@('list volume');$commands | diskpart}

$part comes back as a System.Array and I can't seem to do much to separate the strands.

I can't seem to find the correct WMI call to retrieve the Volume information. Does anyone know what it is?

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2020-12-11T06:52:11.127+00:00

    Hi,

    The diskpart volume number cannot be found in the WMI class Win32_Volume. You have to parse the output of diskpart

    $letter = "C"  
    $command = "select vol $letter"    
    Invoke-Command -Computername $vm -scriptblock {  
        $using:command | diskpart| foreach{  
            if($_ -match "Volume \d+"){  
                $Matches.Values  
            }  
        }   
    }  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Alex Palmer 21 Reputation points
    2020-12-11T08:14:44.283+00:00

    Awesome. Thanks a lot :-)

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.