#PSTip Ejecting and closing CDROM drive–the PowerShell way!

Note: This tip requires PowerShell 2.0 or above.

I came across a project on Github (nothing related to PowerShell though) that provides a web-based UI for running system commands. This is based on Node.js implementation and does not support Windows platform yet. So, I was thinking about adding some PowerShell support.

I started discussing this with a few folks and they suggested an alternative for running system commands on Windows. While looking at that, I stumbled upon the CDROM eject and close commands supported by this 3rd-party tool. I wanted to see how easy that is to do in PowerShell. So, here is what I came up with.

Add-Type -TypeDefinition  @'
using System;
using System.Runtime.InteropServices;
using System.ComponentModel;

namespace CDROM
    public class Commands
        static extern Int32 mciSendString(string command, string buffer, int bufferSize, IntPtr hwndCallback);

        public static void Eject()
             string rt = "";
             mciSendString("set CDAudio door open", rt, 127, IntPtr.Zero);

        public static void Close()
             string rt = "";
             mciSendString("set CDAudio door closed", rt, 127, IntPtr.Zero);


The above snippet uses the .NET interop namespace to invoke a Win32 API for working with multimedia devices. Btw, I came across several blog posts on using WScript Shell and Windows Media Player OCX to achieve this task. But, like I mentioned, this is more PowerShell way of doing it. Drop a comment if you are aware of any alternative methods.

About the author: Ravikanth C

Ravikanth is the founder and editor of the PowerShell Magazine. He is also a PowerShell MVP who works at Dell Inc. He blogs at http://www.ravichaganti.com/blog and you can follow him on Twitter @ravikanth.

Related Posts

%d bloggers like this: