Windows – Finding the drive letter of the most recently mounted xvhd or vhd file in batch

batchcommand linemountwindowswindows 10

I found these handy ways to mount/unmount a xvhd or vhd file in a command prompt / batch file.

However; that still leaves me with one question; assuming I'm specifying the file I want to mount directly; how do I find out what drive letter it was assigned using the command prompt?

So if I can go into DiskPart and run the following commands:

DISKPART> select vdisk FILE="F:\WindowsImageBackup\leeand00-pc\Backup 2017-01-10 031515\924cde0a-0000-0000-0000-50c603000000.vhdx"

DiskPart successfully selected the virtual disk file.


  100 percent completed


  Volume ###  Ltr  Label        Fs     Type        Size     Status     Info
  ----------  ---  -----------  -----  ----------  -------  ---------  --------
  Volume 0     E                       DVD-ROM         0 B  No Media
  Volume 1     H                       DVD-ROM         0 B  No Media
  Volume 2         SYSTEM RESE  NTFS   Partition    100 MB  Healthy    System
  Volume 3     C                NTFS   Partition    159 GB  Healthy    Boot
  Volume 4     F   wbadmin_bac  NTFS   Partition     57 GB  Healthy
  Volume 5         PQSERVICE    NTFS   Partition     15 GB  Healthy    Hidden
  Volume 6     G   FreeAgent D  NTFS   Partition   1397 GB  Healthy
  Volume 7     D                       Removable       0 B  No Media
  Volume 8                      NTFS   Partition    159 GB  Healthy


There is no volume specified.
Please select a volume and try again.

Then I can't assign the drive letter immediately to the newly attached volume…however, I can select it by calling it out by it's volume number:


Volume 8 is the selected volume.


DiskPart successfully assigned the drive letter or mount point.

Seeing as I want to run this as a script…how is it that I can select the newly mounted xvhd and assign it a drive letter?

People screw around with the drive letter system all the time, by mapping and unmapping shares, and by inserting / removing thumb drives.

So how can I safely do this considering the issues with the drive mapping being probably inconsistent, and the fact that when I create a system state backup, it doesn't assign my volume a label?

P.S. My goal here is to automate this so it can be called by a script, do a system state backup (which I've already succeeded at), and then mount the xvhd of the drive, delete most of the user profiles from it (handled by a separate backup) and then unmount it so it can be backed up by another program, and restored to the machine in the event of a disaster; (also I would restore the user profiles from a separate backup).

Best Answer

Finding the drive letter of the most recently mounted xvhd or vhd file in batch? How do I find out what drive letter it was assigned using the command prompt?

Pipe the output of Get-DiskImage to Get-Volume or Get-Disk and Get-Partition:

For an ISO file:


For a VHD file:




Based on the article you provided I incorporated the PowerShell logic into the below batch script to include dynamically getting the drive letter used to mount the VHD or ISO file so you can then use that as a variable name to CD to the directory to run your command implicitly or you can use it otherwise to explicitly set the drive letter for whatever you need it for the commands you'll use it with, etc.




SET DiskPartScript="%TEMP%DiskpartScript.txt"

ECHO SELECT VDISK FILE="%~1" > %DiskPartScript% 
ECHO ATTACH VDISK >> %DiskPartScript%

DiskPart /s %DiskPartScript%

SET PowershellScript="%TEMP%\PowerShellScript.ps1"

:: -- Use below for VHD 
ECHO GET-DISKIMAGE "%~1" | GET-DISK | GET-PARTITION | Select -ExpandProperty DriveLetter > "%PowershellScript%" 

:: -- Use below for ISO 
::ECHO GET-DISKIMAGE "%~1" | get-VOLUME | Select -ExpandProperty DriveLetter > "%PowershellScript%"

SET PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0 
CD /D "%PowerShellDir%" 
FOR /F "DELIMS=" %%A IN ('Powershell -ExecutionPolicy Bypass -Command "& '%PowershellScript%'"') DO SET "DriveLetter=%%A"

CD /D "%DriveLetter%:\" 


Further Resources