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.
[CmdletBinding()] param( [switch]$Eject, [switch]$Close ) try { $Diskmaster = New-Object -ComObject IMAPI2.MsftDiscMaster2 $DiskRecorder = New-Object -ComObject IMAPI2.MsftDiscRecorder2 $DiskRecorder.InitializeDiscRecorder($DiskMaster) if ($Eject) { $DiskRecorder.EjectMedia() } elseif($Close) { $DiskRecorder.CloseTray() } } 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