Created
January 5, 2012 02:28
-
-
Save nicjansma/9b82f28a77f306b0cfc0 to your computer and use it in GitHub Desktop.
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 |
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
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
ormklink
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.