Last active
January 29, 2020 13:46
-
-
Save dadatuputi/94bbaec6e16da0045c425277bac6db54 to your computer and use it in GitHub Desktop.
Powershell - Mount and unlock Bitlocker VHD
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
# 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 | |
} | |
# Mount 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 | |
# $diskimage = Mount-VHD -path $path -PassThru | |
## Diskpart Method | |
# Use this if you don't want to install Hyper-V | |
$results = @( | |
"select vdisk file=$path", | |
"attach vdisk" | |
) | diskpart | |
$diskimage = Get-DiskImage $path | |
# Find volume with drive letter | |
$volumes = $diskimage | Get-Disk | Get-Partition | Get-Volume | |
# Mount bitlocker volume by drive letter, looping until Unlock-BitLocker doesn't return an error | |
while (!$bitlocker) { | |
$bitlocker = Unlock-BitLocker -MountPoint $volumes.DriveLetter -Password (Read-Host 'Enter Password' -AsSecureString) | |
} | |
Invoke-Item $bitlocker.MountPoint | |
# 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