5

#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! :)

Filed in: Columns, Tips and Tricks Tags: ,
  • Pingback: Microsoft Most Valuable Professional (MVP) – Best Posts of the Week around Windows Server, Exchange, SystemCenter and more – #19 - TechCenter - Blog - TechCenter – Dell Community

  • kroonder

    How about:
    $diskNo=(Mount-VHD -Path C:testTest.vhd –Passthru).DiskNumber
    $driveLetter=(Get-Disk $diskNo | Get-Partition).DriveLetter

    You can also check for ‘online’ status (to see if the disk is online or not):
    if ((Get-Disk $diskNo).OperationalStatus -ne ‘Online’)

    Although the compare before/afer is cool and all…

    George/

    • http://www.ravichaganti.com/blog Ravikanth

      Nice one, George! Thanks for sharing.

  • Dene Delower

    Hi Ravikanth ,thanks i might need to use this , Is there a way to brouse or read the vhd file without mounting it, I have a need to scan vhd drives on our virtual machines to find unlicensed software. cheers Dene

    • http://www.ravichaganti.com/blog Ravikanth

      NO! For all practical purposes, VHD is another file. There is no way to read it without opening it first. The way you open a VHD file is to mount it.

© 2013 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.