7

#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: ,

7 Responses to "#PSTip Finding the drive letter of a mounted disk image"

  1. kroonder says:

    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/

  2. Dene Delower says:

    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

    • Ravikanth says:

      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.

  3. Dmitry says:

    Microsoft, so Microsoft. So there is NO way to specify the mount letter, but there is a way to know it…

  4. KERR says:

    This worked for me:

    $diskNo=(Mount-DiskImage -ImagePath “\\server\share\image.iso” –Passthru).Number
    $driveLetter=(Get-Disk $diskNo | Get-Partition).DriveLetter
    $driveLetter

Leave a Reply

Submit Comment

© 2016 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: