Finding Unused Drives

by Oct 22, 2009

You can use a cmdlet called Test-Path to test whether a given path exists. However, existing drives not currently in use, such as a CD-ROM drive, will be reported as not present. Therefore, test-path would never report a CD-ROM drive as present when no media is inserted.

There are numerous ways to work around this. One is the WMI class Win32_LogicalDisk. It gets you the information required:

Get-WmiObject Win32_LogicalDisk | fl *
Get-WmiObject Win32_LogicalDisk |
Format-Table DeviceID, Description, FileSystem, Providername

For example, to use this data to find existing drives by simply placing the drive letter into an array like this:

$drives = Get-WmiObject Win32_LogicalDisk |
Foreach-Object { $_.DeviceID }

Next, you can use the -contains operator to check whether a specific drive exists:

$drives -contains 'C:'
$drives -contains 'L:'

Twitter This Tip! ReTweet this Tip!