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).


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

| improve this answer | |
  • You should be able to use the GET-DISKIMAGE filename.vhd | GET-DISK | GET-PARTITION syntax and just incorporate it into the dynamic script to run with Powershell accordingly. I'm making an assumption you are familiar with batch scripts and Powershell but let me know if you need assist with that part -- this should an easy peezy task. – Pimp Juice IT Jan 10 '17 at 4:33
  • Ah! Sorry I changed some stuff...about what I already tried...so you're saying I should use PowerShell instead? – leeand00 Jan 10 '17 at 4:36
  • 1
    @leeand00 Just incorporate the Powershell commands into the batch script accordingly. I will add an updated example... give me a second... – Pimp Juice IT Jan 10 '17 at 4:42
  • @leeand00 No problem, I had to take care of some things so sorry for the wait. Hopefully that will help you be able to run from a batch script accordingly. I use it similar with other scripts for my needs in some cases. I've just told it to set the drive letter as a variable and then to do a CD /D to that path but you can also use it as "%DriveLetter%:\" and use that for the drive letter part in your explicitly specified paths in your scripted logic. Let me know how it goes when you get to it. I commented out the ISO mount logic but left it there in case someone else uses ISO and needs it. – Pimp Juice IT Jan 10 '17 at 5:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.