Create a gist now

Instantly share code, notes, and snippets.

MountVHD and UnMountVHD: Allows you to mount .VHDs in Windows 7 from the command-line. http://nicj.net/2012/01/04/mounting-vhds-in-windows-7-from-a-command-line-script
@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 dispart 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
@echo off
setlocal enabledelayedexpansion
if {%1}=={} (
echo Usage: %~nx0 [vhd]
exit /b 1
)
set vhdPath=%~dpnx1
echo Unmounting !vhdPath!
REM
REM create dispart script
REM
set diskPartScript=%~nx0.diskpart
echo sel vdisk file="!vhdPath!">!diskPartScript!
echo detach vdisk>>!diskPartScript!
REM
REM diskpart
REM
diskpart /s !diskPartScript!
del /q !diskPartScript!
echo Done!
endlocal
@omidkrad
omidkrad commented May 5, 2014

This is very useful. Thank you. Best part of it is that you don't need to worry about the generated diskpart script. Only a very picky preference I have is I would keep the usage syntax more inline with subst or mklink commands where the drive letter comes first (with a colon) and target path comes as second parameter. Though, I understand that drive letter is optional here and can be omitted.

@SylvainWhissell

This is sweet! Thanks for putting these scripts up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment