The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 2,804 other followers

windows 7 – How can I eject a CD via the cmd? – Super User

Posted by jpluimers on 2021/12/30

Quite a while ago I found [Wayback] windows 7 – How can I eject a CD via the cmd? – Super User, but forgot to document that in the batch-files I created from it.

It shows both this one-liner:

powershell "(new-object -COM Shell.Application).NameSpace(17).ParseName('D:').InvokeVerb('Eject')"

The hardcoded const 17 is for the ssfDRIVES element in the ShellSpecialFolderConstants, which is documented at [Wayback] ShellSpecialFolderConstants (shldisp.h) – Win32 apps | Microsoft Docs.

There is no PowerShell equivalent of that element, hence the hardcoded value 17.

The script invokes the verb Eject, which works on any kind of removable media (not just optical drives). If you want to limit it to only certain drive types, then you would need to compare the Type of the ParseName() result. However, that result has a Type property returns a string for which the possible values are not documented.

Here are some links I tried to find out what is returned:

In addition to the Shell.Application, there also is Scripting.FileSystemObject, which allows enumerating the drives and filter on DriveType. This is the relevant documentation:

The second example in the above mentioned answer shows how to use this to filter for optical drives.

It also shows a cool technique to have a hybrid batch-file/JScript script:

@if (@CodeSection == @Batch) @then

@echo off

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

// NameSpace(17) = ssfDRIVES, or My Computer.
// See

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)


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: