Q: What is my VHD file's drive letter?

A: If you mounted a virtual hard disk (VHD) and want to check its drive letter, use the following two Windows PowerShell commands:

 

<code>$DiskNumber = (Get-VHD “d:\temp\temp2gb.vhdx”).DiskNumber<br>
$DriveLetter = (gwmi Win32_DiskPartition -filter "DeviceID like 'Disk #$DiskNumber,%'").PSBase.GetRelated('Win32_LogicalDisk') | Select-object -ExpandProperty DeviceID</code>

Another option using PowerShell, is to enter this:

<code>$disks = Get-CimInstance -ClassName Win32_DiskDrive | where Caption -eq "Microsoft Virtual Disk" <br></code><code>foreach ($disk in $disks){ <br>
   $vols = Get-CimAssociatedInstance -CimInstance $disk -ResultClassName Win32_DiskPartition <br>
   foreach ($vol in $vols){ <br>
       Get-CimAssociatedInstance -CimInstance $vol -ResultClassName Win32_LogicalDisk | <br>
          where VolumeName -ne 'System Reserved' <br>
   } <br></code>}

Here's an example of output I received:

DeviceID DriveType ProviderName VolumeName Size FreeSpace -------- --------- ------------ ---------- ---- --------- N: 3 TempVHD 2111827968

Discuss this Article 2

kiwizz
on Dec 20, 2012
Hi John, your first example is exactly what I need - to be able to identify the drive letter based on the filename of the vhdx that is attached. I am getting the following error though: Method invocation failed because [System.Management.Automation.PSInternalMemberSet] doesn't contain a method named 'GetRelated'. Am I missing something? Could it be because I am using server 2012? Thanks!

Please or Register to post comments.

Upcoming Training

Mastering System Center 2012

During over 6 hours of training you can join John Savill from your computer as he will walk you through the key components and capabilities of System Center 2012, what’s involved in using the components, and the benefit they can bring to your environment.

Register Now

Current Issue

May 2013 - The NameTranslate object is useful when you need to translate Active Directory object names between different formats, but it's awkward to use from PowerShell. Here's a PowerShell script that eliminates the awkwardness.

CURRENT ISSUE / ARCHIVE / SUBSCRIBE

Windows Forums

Get answers to questions, share tips, and engage with the Windows Community in our Forums.