Windows 7 command script to mount VHD disk with assigned drive letter via DiskPart
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
Related videos on Youtube
Nam G VU
Updated on September 18, 2022Comments
-
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 withDisk Manager
tool e.g. guided here.My google search came up with
DiskPart
command; though I cannot setdrive letter
without specifyingvolume 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.
-
Vomit IT - Chunky Mess Style about 7 yearsNam - 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 about 7 yearsHere'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 over 8 yearsThe
select partition 1
diskpart command not works for me. -
Dawn Benton over 8 yearsdoes it give an error?
-
Nam G VU over 8 yearsYes, kind of
the volumn id not select
-
Dawn Benton over 8 yearsif you launch diskpart manually and do sel vdisk file = "path to vdisk file" and then do list partition .. what does it show?
-
Dawn Benton over 8 yearsAlso, I've edited my answer above, I apologize. When specifying a drive letter to mountvhd.cmd, don't use the colon (:)
-
Nam G VU over 8 yearsI will try again and back to you later
-
BGunnells almost 7 yearsThis 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.