Find CD Drive Letter(s)

August 10, 2012 1 comment

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",""
$cddrive
Advertisements
Categories: Windows OS

Hello WordPress world!

August 10, 2012 2 comments

Welcome to my blog. After nearly 10 years of doing system admin work, I have finally decided to start a blog discussing what I do.

I hope you all enjoy my tidbits about Powershell and other system admin related topics.

 

Categories: Uncategorized