AnEnglishmanInNorway
AnEnglishmanInNorway

Reputation: 105

How do I tell if a drive is a dvd or virtual drive, test-path isn't enough?

As part of learning to use PowerShell, I have been examining how to locate a free drive letter. This SO question is a great startpoint: However, if there is a DVD drive and/or a virtual drive on the PC, then the really neat pipeline

ls function:[d-z]: -n | where { !(test-path $_) } 

will claim that the letters for those drives are free (because there is nothing mounted). Is there some simple algorihmic way to take those drives out of the list - assuming that the command must work on all machines, and not use explicit knowledge of what drives exist?

Upvotes: 3

Views: 1591

Answers (1)

Matt
Matt

Reputation: 46730

The WMI class Win32_CDROMDrive already knows the cd/dvd rom drives that exist. Using the information from that we can exclude those generate drive list.

$cddrives = (Get-WmiObject Win32_CDROMDrive).Drive
ls function:[d-z]: -N | Where-Object {$cddrives -notcontains $_ -and !(test-path $_)}

FYI ls = Get-ChildItem and -N is the unambiguous short reference to the parameter -Name

Upvotes: 2

Related Questions