#PSTip Finding the drive letter of a mounted disk image

Note: This tip requires PowerShell 3.0 or above.

Windows 8 and Windows Server 2012 included cmdlets to manage storage. One such cmdlet is Mount-DiskImage which can be used to mount ISO, VHD, and VHDX files. But, unfortunately, this cmdlet does not give us information on the drive letter assigned to the newly mounted disk image. Knowing the drive letter might be a very important thing when using these cmdlets in a script.

So, how do we find the drive letter? Here is one method:

$before = (Get-Volume).DriveLetter
Mount-DiskImage -ImagePath C:\test\Test.vhd -StorageType VHD
$after = (Get-Volume).DriveLetter
compare $before $after -Passthru

drive

There are other methods too–such as using PowerShell eventing or background jobs, etc. Let us save that for future tips! 🙂

About the author: Ravikanth C

Ravikanth is the founder and editor of the PowerShell Magazine. He is also a PowerShell MVP who works at Dell Inc. He blogs at http://www.ravichaganti.com/blog and you can follow him on Twitter @ravikanth.

Related Posts

%d bloggers like this: