I've always wondered why Windows has an Eject option but doesn't include a corresponding Close / Close Tray option in the context menu of optical drives.
Is there a way of closing the optical drive tray without the use of any third party software in Windows?
Best Answer
The only way to do this IMO without the use of 3rd party utils (such as NirCmd and Wizmo) would be via VBScript or PowerShell. All the VBScript solutions I've seen so far use an outdated Windows Media Player OCX. I don't know whether the latest versions of WMP include an OCX with similar functionality or not, plus disabling/uninstalling it via Windows Features might interfere with the functioning of the script in any case.
A common way to implement this functionality via code is by using the Media Control Interface (MCI) APIs (specifically, the set command). However, since VBScript does not support calling normal Windows API functions or even functions from arbitrary DLLs, that leaves us with PowerShell. Thus the following should work out of the box in Windows 7+ that comes with PS pre-installed, and on XP/Vista after PS is installed. The MCI DLL i.e. Windows\System32\WinMM.dll should be available as part of the default installation in XP+.
1) Save the following as CD_Open.ps1:
2) Save the following as CD_Close.ps1:
Now comes the problem. By default, unsigned PS scripts cannot be executed in Windows for security reasons. Type
get-help about_signing
at the PS prompt to know more about this, including how to self sign your scripts and so on.Luckily there's a workaround as the get-help command above states:
3) So from an elevated Command Prompt, run the following command:
(You can run
powershell set-executionpolicy restricted
to revert to the default setting.)This command needs to be run only once and remains in effect until you change the execution policy again.
4) Now you can use the following commands (even from a non-elevated Command Prompt) to open/close the optical drive tray:
Of course, you can create shortcuts as well so that you can open/close the tray with a click or a key combo:
You can also add the Close command to the context menu of your optical drive using the following .REG file:
(Edit the paths as required. Also, the -WindowStyle parameter is only available with PS 2.0+.)