Skip to content

Instantly share code, notes, and snippets.

@sonuame
Forked from nicjansma/MountVHD.cmd
Created March 19, 2018 09:27
Show Gist options
  • Save sonuame/1d3a37fa2fe9f8d8912a6d1743acbe5f to your computer and use it in GitHub Desktop.
Save sonuame/1d3a37fa2fe9f8d8912a6d1743acbe5f 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
@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment