Ejecting CDs

by Apr 24, 2009

PowerShell can still use COM libraries. Simply use New-Object -comObject and provide the COM library name to load it. With this approach, you can do all kinds of unusual things, such as ejecting a CD:

$cd_drive = 'E:'
$sa = New-Object -comObject Shell.Application
$sa.Namespace(17).parseName($cd_drive)
$sa.Namespace(17).ParseName("$cd_drive").InvokeVerb("Eject")

The example demonstrates how to call context menu items on shell items. Namespace(17) is your computers folder, and ParseName() finds any item available in it by name. With InvokeVerb() you can then call any command. Just make sure you adjust the drive letter in $cd_drive accordingly.

To open the properties of your CD drive, simply change the verb like so:

$sa.Namespace(17).ParseName("$cd_drive").InvokeVerb("Properties")

Note: Property names can be hard to guess because they may be localized. If you want to see all available context menu verbs, try this:

$sa.Namespace(17).ParseName("$cd_drive").Verbs()