Find CD Drive Letter(s)

I was presented with an issue the other day where I needed to mount an ISO to a VM, then run a program which was housed on that CD Drive. I could not find a central, easy way using PowerShell to find what the letter the CD Drive was so I decided I would create one. I know this could have been done with WMI, but I decided I would go a different route for this occasion since I have used FSUTIL for other items in the past.

First off we start off by querying all drives on the machine using the FSUTIL command, we then pipe these results to our variable of $drives. Line 1 will give us a result of the drive letters that are in use on the machine. With line 2 we are actually getting rid of part of the content of the $drives variable. We do not want to have the “Drives: ” content in our variable. Now that this is complete we can then split up our $drives variable into an array. We do this by using the .split function. This allows us to break up our variable into multiple parts, in this case we are telling PowerShell to break up the $drives variable everywhere there is a space, this is indicated by the ” ” portion.

Now that we have broken our variable up into an array we can now perform a foreach loop to determine each drive letters type. This foreach loop can be completed by typing “ForEach” or “%” either will start the loop. Once that loop completes writing the results to the $drivetype variable we can then process each CD Drive that resides on the host machine.

Once complete you should have $cddrive representing the drive letter (i.e. E:) of your CD Drive or Drives.

$drives = fsutil fsinfo drives
$drives = $drives -replace "drives: ",""
$drives = $drives |%{$_.split(" ")}
$drivetype= $drives | %{fsutil fsinfo drivetype $_}
$cddrive = $drivetype | where {$_ -like "*CD*"}
$cddrive = $cddrive -replace " - CD-ROM Drive",""
Posted in Windows OS.


  1. Did you include the System.Management.dll as a rercfenee in your project? Also do you have a using System.Management in the file that you’re trying this from.I assume the answer to both of those is yes, because those both should be compile time errors.What version of .NET are you running? I tried this out in .NET 3.5 and it worked. I just looked them up on MSDN and the namespaces are the same in .NET 4.0.Also, what line are you getting the exception on? (Is it the foreach line?)

Leave a Reply

Your email address will not be published. Required fields are marked *