How can I eject a CD via the cmd?
You can eject a cd with a batch file (this is part vbscript
@echo off
echo Set oWMP = CreateObject("WMPlayer.OCX.7") >> %temp%\temp.vbs
echo Set colCDROMs = oWMP.cdromCollection >> %temp%\temp.vbs
echo For i = 0 to colCDROMs.Count-1 >> %temp%\temp.vbs
echo colCDROMs.Item(i).Eject >> %temp%\temp.vbs
echo next >> %temp%\temp.vbs
echo oWMP.close >> %temp%\temp.vbs
%temp%\temp.vbs
timeout /t 1
del %temp%\temp.vbs
This is not my work, I found it in the stackoverflow community:
Post Link: Batch Command Line to Eject CD Tray?
Answer Author: Bruno
Date Answered: Feb 10, 2015
You could use the Shell.Application
COM object's InvokeVerb
method. From a cmd prompt, you can abuse a PowerShell one-liner thusly:
powershell "(new-object -COM Shell.Application).NameSpace(17).ParseName('D:').InvokeVerb('Eject')"
You can also use Windows Scripting Host (VBScript / JScript) to invoke the COM object. Here's an example using a hybrid Batch + Jscript script (save it with a .bat extension):
@if (@CodeSection == @Batch) @then
@echo off
setlocal
set "CDdrive=D:"
cscript /nologo /e:JScript "%~f0" "%CDdrive%"
goto :EOF
@end // end batch / begin JScript hybrid chimera
var oSH = WSH.CreateObject('Shell.Application');
oSH.NameSpace(17).ParseName(WSH.Arguments(0)).InvokeVerb('Eject');
If you prefer to have your script detect the drive letter for the CD drive, that can be arranged as well. Here's a more complete version with comments explaining some of the non-self-explanatory values.
@if (@CodeSection == @Batch) @then
@echo off
setlocal
cscript /nologo /e:JScript "%~f0"
goto :EOF
@end // end batch / begin JScript hybrid chimera
// DriveType=4 means CD drive for a WScript FSO object.
// See http://msdn.microsoft.com/en-us/library/ys4ctaz0%28v=vs.84%29.aspx
// NameSpace(17) = ssfDRIVES, or My Computer.
// See http://msdn.microsoft.com/en-us/library/windows/desktop/bb774096%28v=vs.85%29.aspx
var oSH = new ActiveXObject('Shell.Application'),
FSO = new ActiveXObject('Scripting.FileSystemObject'),
CDdriveType = 4,
ssfDRIVES = 17,
drives = new Enumerator(FSO.Drives);
while (!drives.atEnd()) {
var x = drives.item();
if (x.DriveType == CDdriveType) {
oSH.NameSpace(ssfDRIVES).ParseName(x.DriveLetter + ':').InvokeVerb('Eject');
while (x.IsReady)
WSH.Sleep(50);
}
drives.moveNext();
}
Command line CD-eject oneliner:
In a bat file or directly in cmd
this worked after first run of wmplayer
on Windows 8:
powershell (New-Object -com "WMPlayer.OCX.7").cdromcollection.item(0).eject()