Windows 7 command script to mount VHD disk with assigned drive letter via DiskPart

8,966

I use a script which I found on the Internet a long time ago.

This script (MountVHD.cmd) generates a diskpart script and then calls diskpart with the generated script using the path and drive letter that you specify.

This script accepts two parameters:

MountVHD.cmd \path\to\vhdfile.vhd X

where X: is the drive letter to assign.

You can then create another command script which calls this with the command line above and place that calling script in the startup folder.

@echo off
setlocal enabledelayedexpansion

if {%1}=={} (
    echo Usage: %~nx0 [vhd] [letter]
    exit /b 1
)
set vhdPath=%~dpnx1
set driveLetter=%2

if {!driveLetter!}=={} (
    echo Mounting !vhdPath!
) else (
    echo Mounting !vhdPath! to !driveLetter!:
)

REM
REM create diskpart script
REM
set diskPartScript=%~nx0.diskpart
echo sel vdisk file="!vhdPath!">!diskPartScript!
echo attach vdisk>>!diskPartScript!

REM assign the drive letter if requested
if not {!driveLetter!}=={} (
    echo select partition 1 >>!diskPartScript!
    echo assign letter=!driveLetter!>>!diskPartScript!
)

REM Show script
echo.
echo Running diskpart script:
type !diskPartScript!

REM
REM diskpart
REM
diskpart /s !diskPartScript!
del /q !diskPartScript!

echo Done!

endlocal
Share:
8,966

Related videos on Youtube

Nam G VU
Author by

Nam G VU

Updated on September 18, 2022

Comments

  • Nam G VU
    Nam G VU almost 2 years

    I use VHD disk for storing my Dropbox files on my office PC.

    I need the .vhd to be loaded automatically when booted i.e. I will just have to enter decode password instead of going all the way with Disk Manager tool e.g. guided here.

    My google search came up with DiskPart command; though I cannot set drive letter without specifying volume id parameter. More google search came up with this thread, though using Powershell is not a native/favor way for me. I need a native Windows command that just works.

    So my question is how to get volume id of a mounted VHD disk when calling DiskPart? Currently I have to set it as 3 which only applicable for my PC.

    p.s.

    1. Not looking for Powershell scripts
    2. My DiskPart script can be viewed here 1) mount, 2) unmount
    • Vomit IT - Chunky Mess Style
      Vomit IT - Chunky Mess Style about 7 years
      Nam - I see a couple answer to this question and you've not accepted any. I wanted to share an answer of mine with you that runs pure CMD batch that may be helpful in your task with appropriate adjustments. While this "batch" script does use PowerShell functionality to get the information and allow the batch script to use it, you do NOT need to run it as a PowerShell script or from PowerShell command or IDE, you just run it from batch and use the more robust PS commands accordingly to get the needed information rather than a ton of batch logic for such a simple task.
    • Vomit IT - Chunky Mess Style
      Vomit IT - Chunky Mess Style about 7 years
      Here's the post to my answer I mention above: superuser.com/questions/1165369/…. I encourage you to a least look at it just in case you find it helpful.
  • Nam G VU
    Nam G VU over 8 years
    The select partition 1 diskpart command not works for me.
  • Dawn Benton
    Dawn Benton over 8 years
    does it give an error?
  • Nam G VU
    Nam G VU over 8 years
    Yes, kind of the volumn id not select
  • Dawn Benton
    Dawn Benton over 8 years
    if you launch diskpart manually and do sel vdisk file = "path to vdisk file" and then do list partition .. what does it show?
  • Dawn Benton
    Dawn Benton over 8 years
    Also, I've edited my answer above, I apologize. When specifying a drive letter to mountvhd.cmd, don't use the colon (:)
  • Nam G VU
    Nam G VU over 8 years
    I will try again and back to you later
  • BGunnells
    BGunnells almost 7 years
    This answers the OP's original question of "how to get volume id of a mounted VHD disk when calling DiskPart". The other answers assume the Volume ID is always 1 -- see where each script contains the line [...] select partition 1. I found, however, that Diskpart did not always assign the same Volume Number to my virtual disk. Therefore, regardless of the Volume Number assigned to my VHD, my script will find the correct volume and assign the preferred drive letter to it.