Skip navigation

Q: How can I use the Windows PowerShell cmdlets for Hyper-V to eject all ISOs from the DVD drive?

A: If you want to eject an ISO from a DVD drive or even just disconnect a virtual hard disk (VHD) from a controller, it’s not obvious what actual cmdlet to use.

You need to use the Remove-VMDrive cmdlet, which removes the entire device from the virtual machine (VM). You must also add the -Diskonly switch, which just unmounts. This is assuming you have installed the Microsoft Hyper-V cmdlets from CodePlex (http://pshyperv.codeplex.com/).

Below, you can see an example of how to import them into the PowerShell instance, and, in the same command, eject whatever ISO is connected to the DVD drive on a VM, where the VM, named VM1, is on IDE controller 1, device 0:

Import-Module "c:\Program Files\modules\HyperV\Hyperv.psd1"
Remove-VMDrive -Diskonly -VM VM1 1 0

If you want to go one step further and eject ISOs from all your VMs on your box, you could use the following:

get-vm | foreach {Remove-VMDrive -Diskonly -VM $_.vmelementName 1 0}
You will see an OK for each VM with ISO disconnected. If you want to disconnect only a specific ISO file from all the VMs, you can enumerate all the virtual disks and search for the ISO using this command:
PS C:\> get-vm | get-vmdisk | where {$_.diskpath -eq "D:\Library\ISOs\Software\en_lync_server_2010_x64_dvd_598415.iso"}

It returns this output:

VMElementName : savdalwss10 VMGUID : E28694D7-E4C3-4F98-9BF9-CD6A40F932EB
ControllerName : IDE Controller 1
ControllerInstanceID : Microsoft:E28694D7-E4C3-4F98-9BF9-CD6A40F932EB\83F8638B-8DCA-4152-9EDA-2CA8B33039B4\1
ControllerID : 1 DriveName : DVD Drive
DriveInstanceID : Microsoft:E28694D7-E4C3-4F98-9BF9-CD6A40F932EB\83F8638B-8DCA-4152-9EDA-2CA8B33039B4\1\0\D
DriveLUN : 0 DiskPath : D:\Library\ISOs\Software\en_lync_server_2010_x64_dvd_598415.iso
DiskImage : D:\Library\ISOs\Software\en_lync_server_2010_x64_dvd_598415.iso
DiskName : ISO
Disk Image DiskInstanceID : Microsoft:E28694D7-E4C3-4F98-9BF9-CD6A40F932EB\83F8638B-8DCA-4152-9EDA-2CA8B33039B4\1\0\L

To actually disconnect, you just add the Remove-VMDrive foreach loop to the end, but this time you can use the controller ID and LUN ID that resulted from the get-vmdisk command. This way, you can disconnect the ISO no matter what devices it’s mounted on:

PS C:\> Get-VM | Get-VMDisk | where {$_.diskpath -eq "D:\Library\ISOs\Software\en_ly
nc_server_2010_x64_dvd_598415.iso"} | foreach {Remove-VMDrive -Diskonly -VM $_.vmelementName $_.ControllerID $_.DriveLUN}

To read more FAQs about all things Windows, see John Savill's FAQs page.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish