5

Is this possible?

My first guess would be something like:

C:> Get-WmiObject Win32_CDROMDrive

But when I tried this, it only tells me Caption, Drive, Manufacturer,VolumeName

No information on whether or not there is a CD in the disc drive.

Kellen Stuart
  • 7,775
  • 7
  • 59
  • 82
  • 3
    `[IO.DriveInfo]::GetDrives() | where { $_.DriveType -eq 'CDRom' -and $_.IsReady }` (based on C# answers) – wOxxOm Jun 20 '17 at 18:08
  • 3
    Personally, I find questions like this misleading. Is there a cd in the drive, yes, when you checked, but 1 second later, the user ejected it. Why not just try to read (or write) to the drive or whatever you want to do, and handle failures? – Kory Gill Jun 20 '17 at 18:11
  • @KoryGill Reading or writing to the CD is beside the point. The end goal is to check to see if there is media inside the drive or not. – Kellen Stuart Jun 20 '17 at 19:23
  • 1
    What problem are you solving by getting the answer to this question? – Bill_Stewart Jun 20 '17 at 19:24

1 Answers1

5

You can get this information by

(Get-WMIObject -Class Win32_CDROMDrive -Property *).MediaLoaded

You can see what properties are available for that WMI class by

Get-WMIObject -Class Win32_CDROMDrive -Property * | Get-Member

and more detailed documentation from

Get-WMIHelp -Class Win32_CDROMDrive

In general, you will find that liberal use of the Get-Help, Get-Member, Get-Command, and Get-WMIHelp cmdlets will provide you with a great deal of information, and possibly eliminate the need to ask questions like this here and wait for an answer that may or may not come.

Jeff Zeitlin
  • 9,773
  • 2
  • 21
  • 33
  • Ah, should've tried `-Properties *`. I always forget there may be properties it is not reporting – Kellen Stuart Jun 20 '17 at 19:24
  • 1
    Get-WMIHelp is not a standard PowerShell cmdlet. – Adam Mnich Jun 20 '17 at 21:04
  • True. See https://blogs.msdn.microsoft.com/powershell/2007/09/24/get-wmihelp-amp-search-wmihelp/ and https://ss64.org/viewtopic.php?id=1440 - I've had them on my system for so long that I'd forgotten they were add-ons. – Jeff Zeitlin Jun 21 '17 at 11:15