Ejecting CD Drive

Here is a fun little function that uses WMI to eject your CD drive. It does so by first asking WMI for all CD drives. It then uses the explorer object model to navigate to the drive and call its context menu item “Eject”:

function Eject-CD
  $drives = Get-WmiObject Win32_Volume -Filter "DriveType=5"
  if ($drives -eq $null)
    Write-Warning "Your computer has no CD drives to eject."
  $drives | ForEach-Object {
    (New-Object -ComObject Shell.Application).Namespace(17).ParseName($_.Name).InvokeVerb("Eject")


Twitter This Tip! ReTweet this Tip!

  • This is fun, but shouldn't you use an approved Windows PowerShell verb? If so, your function could be "Open-CD" to use an approved verb.