0

Powershell: Initialise and mount VHD disks

Using Powershell and VBoxManage (VirtualBox), I can create and mount a VHD disk, as follows:

$vhdpath="c:\myvhd.vhd"
vboxmanage createmedium  disk --filename $vhdpath --sizebyte 200 --format VHD --variant Fixed
$vhd = Mount-DiskImage -PassThru $vhdpath -StorageType VHD 
if(-not $vhd) {
    Write-Host "Error mounting VHD"
    exit
}
$vhd=Get-DiskImage -ImagePath $vhd.ImagePath
Initialize-Disk -Number $vhd.Number -PartitionStyle MBR
$partition = New-Partition -AssignDriveLetter -UseMaximumSize -DiskNumber $vhd.Number
$volume = Format-Volume -FileSystem FAT32  -Confirm:$false -Force -Partition $partition      

Of course it is possible to replace vboxmanage ... with Hyper-V equivalent commands.

Two questions:

  1. When the VHD disk is visible to PS, so is to the OS and, before the PS script formats it, an annoying Windows pop-up appears proposing to format the disk. How can I get rid of it?

  2. In general, it is possible to mount a VHD disk as a removable disk. Is it possible to do so with PS too?

I am primarily (but not necessarily) looking for commands that do not require Hyper-V being installed.

antonio
  • 10,629
  • 13
  • 68
  • 136
  • Possible solution to this here: http://serverfault.com/questions/804470/create-a-new-vhd-and-mount-it-without-diskmanagement-window-popup-with-powershel – henrycarteruk Mar 01 '17 at 14:36
  • @JamesC.: Thanks, but the solution involves installing a closed source tool which disables some Windows low level functionality. Should I go this path, I'd rather use the well known [ImDisk](http://www.ltr-data.se/opencode.html) open source tool, which can create, format VHD and mount them as removable (and without triggering OS popups). Here on SO I am looking for a programmatic solution. – antonio Mar 01 '17 at 23:52

0 Answers0