As the years go by, more and more research, information, and documents can be found online. However, several business verticals still use CDs and DVDs to access research content, backup documents, and share files. In every position that I’ve ever held, I have been asked by someone to locate a disk. I had an old WMI script that would grab the information, place it in a text file on a machine, and notify me via a net message that the file was ready. This was great and did the job, but I wanted to see how I can do the same using PowerShell (and WMI).
When you install PowerShell, it’s capabilities can be expanded through modules. One of the modules that is found in the base installation of PowerShell is the Microsoft.PowerShell.Management module. This module contains a method called Get-WmiObject. This method will use WMI to gain access to certain machine information. In this case, we can obtain the drive information by using the class Win32_LogicalDisk and filtering the drive type to 2 and 5. We’ll choose 2 and 5 as this will return only the Removable Disc and Compact Disc drives. The other drive types are shown below:
Value Meaning 0 Unknown 1 No Root Directory 2 Removable Disk 3 Local Disk 4 Network Drive 5 Compact Disk 6 RAM Disk
From each of these drives, we’ll be able to obtain the DriveID (drive letter) and VolumeName (CD/DVD label or name of the removable device). Finally, we’ll pass this information into a custom psobject to display a result table such as below:
The final script appears as below:
For this script to work, remote RPC needs to be enabled, it should be executed from an administrator account, and it should either be signed or executed by allowing (temporarily) unsigned scripts to be executed. This script has been tested to work with PowerShell v1 so that it will work with legacy versions of PowerShell. With later versions of PowerShell (and potentially with this version), this may be able to be refactored.
Feel free to submit revisions to this Gist.