Get-WMIObject Win32_CDROMdrive | Select MediaLoaded 

does a great job of telling me if there's a CD loaded into my CD drive. It returns this:

MediaLoaded ----------- True

My problem is: I can't figure out how to evaluate this output as boolean "True" or "False". It always seems to be "True" even when the return text says "False".

1 Answer

You don't expand the property, so your Select-Object command gives you an object with one property with a boolean value instead of the actual boolean value. Since PowerShell interprets non-empty/non-zero values as $true you're getting the behavior you observed when you use the object in a boolean context.

Change this:

... | Select MediaLoaded 

into this:

... | Select -Expand MediaLoaded 

and the problem will disappear.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.