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