Search code examples
vbscriptdrivewinpecd-rom

Identify CD drive and eject using bat or vbs while in WinPE without external files


I need to identify the CD drive and eject the tray. This is executed while booted in WinPE, so the WMP eject function is not available. This script will be used on various computer models/configurations. I'm currently using this:

For Each d in CreateObject("Scripting.FileSystemObject").Drives
    CreateObject("Shell.Application").Namespace(17).ParseName("D:\").InvokeVerb("Eject")
Next

It works, but sometimes it errors and requires user interaction before it ejects. I suspect that it's because of the hardcoded D:\ drive letter, but I could be completely wrong. I need this to work without 3rd party utilities.


Solution

  • Use the DriveType property of the Drive object:

    For Each d in CreateObject("Scripting.FileSystemObject").Drives
        WScript.sleep 60
        If d.DriveType = 4 Then
            CreateObject("Shell.Application").Namespace(17).ParseName(d.DriveLetter & ":\").InvokeVerb("Eject")
        End If
    Next