≡ Menu

Eject or Close CD/DVD Drive using PowerShell(alternative to windows media objects)

In past I have written on the same subject. That was based on “WMPlayer.OCX.7” object which seems to be failing in some of the cases. I stumbled on a COM object today which I can use reliably to eject CD/DVD drive without issues. There are some more approaches as well available in internet which use a C# code in PowerShell. They also will work as expected but this is in pure form of powershell.

IMAPI(Image Mastering API) Interface in Windows has several abilities in managing the CD/DVD/BD disk drives and the COM objects I used in my below script are from the same family.

The script is short and straightforward.

try {            
    $Diskmaster = New-Object -ComObject IMAPI2.MsftDiscMaster2            
    $DiskRecorder = New-Object -ComObject IMAPI2.MsftDiscRecorder2            
    if ($Eject) {            
    } elseif($Close) {            
} catch {            
    Write-Error "Failed to operate the disk. Details : $_"            

I published this script on Technet Script library as well. You can download it from there

{ 2 comments… add one }
  • frank June 11, 2014, 11:09 pm

    Great piece of Powershell code.

    Under the elseif ($Close) , I think the Method should be $DiskRecoder.CloseTray() not $EjectMedia()

    Frank :-0)

Leave a Comment