Skip to content

Instantly share code, notes, and snippets.

@dadatuputi
Last active October 14, 2018 21:32
Show Gist options
  • Save dadatuputi/29714b76bbccf99bff34c2c7d23ccc30 to your computer and use it in GitHub Desktop.
Save dadatuputi/29714b76bbccf99bff34c2c7d23ccc30 to your computer and use it in GitHub Desktop.
Powershell - Unmount VHD
# Escalate script if not run as administrator
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
{
# Relaunch as an elevated process:
Start-Process powershell.exe "-File",('"{0}"' -f $MyInvocation.MyCommand.Path) -Verb RunAs
exit
}
# Unmount vhd file
# Use the following path if the vhd file is in the same directory as this script:
# $path = Join-Path $PSScriptRoot my.vhd
$path = "C:\path\to\my.vhd"
## Hyper-V Method
# Use this if you have Hyper-V installed
# Dismount-VHD -path $path
## Diskpart Method
# Use this if you don't want to install Hyper-V
$results = @(
"select vdisk file=$path",
"detach vdisk"
) | diskpart
# References for Diskpart method:
# https://www.top-password.com/blog/mount-and-unmount-vhd-vhdx-from-command-line/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment