WMI Tasks: Disks and File Systems
WMI tasks for disks and file systems obtain information about disk drive hardware state and logical volumes. For other examples, see the TechNet ScriptCenter at http://www.microsoft.com/technet.
The script examples shown in this topic obtain data only from the local computer. For more information about how to use the script to obtain data from remote computers, see Connecting to WMI on a Remote Computer.
The following procedure describes how to run a script.
To run a script
- Copy the code and save it in a file with a .vbs extension. Ensure that your text editor does not add a .txt extension to the file.
- Open a command prompt window and navigate to the directory where you saved the file.
- Type cscript scriptfile.vbs at the command prompt.
Note By default, cscript displays the output of a script in the command prompt window. Because WMI scripts can produce large amounts of output, you might want to redirect the output to a file. Type cscript scriptfile.vbs > outfile.txt at the command prompt to redirect the output of the filename.vbs script to outfile.txt.
The following table lists script examples that can be used to obtain various types of data from the local computer.
| How do I... | WMI classes or methods |
|---|---|
| ...find out how much disk space each user is currently using on a computer? | If you are using disk quotas, then use the Win32_DiskQuota class and retrieve the values of the User and DiskSpaceUsed properties.
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & strComputer & "\root\cimv2") Set colQuotas = objWMIService.ExecQuery _ ("Select * from Win32_DiskQuota") For each objQuota in colQuotas Wscript.Echo "Volume: "& vbTab _ & objQuota.QuotaVolume Wscript.Echo "User: "& vbTab & objQuota.User Wscript.Echo "Disk Space Used: " _ & vbTab & objQuota.DiskSpaceUsed Next |
| ...determine when a removable drive has been added to or removed from a computer? | Use a monitoring script that queries the Win32_VolumeChangeEvent class.
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & strComputer & "\root\cimv2") Set colMonitoredEvents = objWMIService. _ ExecNotificationQuery( _ "Select * from Win32_VolumeChangeEvent") Do Set objLatestEvent = colMonitoredEvents.NextEvent Wscript.Echo objLatestEvent.DriveName Wscript.Echo objLatestEvent.EventType Wscript.Echo objLatestEvent.Time_Created Loop |
| ...determine if a CD is in a CD-ROM drive? | Use the Win32_CDROMDrive class and the MediaLoaded property.
strComputer = "." Set objWMIService = GetObject(_ "winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery( _ "Select * from Win32_CDROMDrive") For Each objItem in colItems Wscript.Echo "Device ID: " _ & objItem.DeviceID Wscript.Echo "Media Loaded: " _ & objItem.MediaLoaded Next |
| ...determine if a disk is in the floppy drive? | Use the Win32_LogicalDisk class and check the FreeSpace property. If the value is Null, then no disk is in the drive.
strComputer = "." Set objWMIService = GetObject( _ "winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery _ ("Select * From Win32_LogicalDisk Where DeviceID = 'A:'") For Each objItem in colItems intFreeSpace = objItem.FreeSpace If IsNull(intFreeSpace) Then Wscript.Echo "There is no disk in the floppy drive." Else Wscript.Echo "There is a disk in the floppy drive." End If Next |
| ...distinguish between a fixed hard disk and a removable hard disk? | Use the Win32_LogicalDisk class and check the value of the DriveType property.
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & strComputer & "\root\cimv2") Set colDisks = objWMIService.ExecQuery _ ("Select * from Win32_LogicalDisk") For Each objDisk in colDisks Wscript.Echo "DeviceID: "& vbTab _ & objDisk.DeviceID Select Case objDisk.DriveType Case 1 Wscript.Echo "No root directory. " _ & "Drive type could not be " _ & "determined." Case 2 Wscript.Echo "DriveType: "& vbTab _ & "Removable drive." Case 3 Wscript.Echo "DriveType: "& vbTab _ & "Local hard disk." Case 4 Wscript.Echo "DriveType: "& vbTab _ & "Network disk." Case 5 Wscript.Echo "DriveType: "& vbTab _ & "Compact disk." Case 6 Wscript.Echo "DriveType: "& vbTab _ & "RAM disk." Case Else Wscript.Echo "Drive type could not be" _ & " determined." End Select Next |
| ...determine what file system is in use on a drive? | Use the Win32_LogicalDisk class and the FileSystem property.
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & strComputer & "\root\cimv2") Set colDisks = objWMIService.ExecQuery _ ("Select * from Win32_LogicalDisk") For Each objDisk in colDisks Wscript.Echo "DeviceID: " & objDisk.DeviceID Wscript.Echo "File System: " _ & objDisk.FileSystem Next |
| ...determine how much free space is available on a drive? | Use the Win32_LogicalDisk class and the FreeSpace property.
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & strComputer & "\root\cimv2") Set colDisks = objWMIService.ExecQuery _ ("Select * from Win32_LogicalDisk") For Each objDisk in colDisks Wscript.Echo "DeviceID: " & objDisk.DeviceID Wscript.Echo "Free Disk Space: " _ & objDisk.FreeSpace Next |
| ...determine the size of a drive? | Use the Win32_LogicalDisk class, and the Size property.
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & strComputer & "\root\cimv2") Set colDisks = objWMIService.ExecQuery _ ("Select * from Win32_LogicalDisk") For Each objDisk in colDisks Wscript.Echo "DeviceID: " & objDisk.DeviceID Wscript.Echo "Disk Size: " & objDisk.Size Next |
| ...find out what drives are mapped on a computer? | Use the Win32_MappedLogicalDisk class.
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & strComputer & "\root\cimv2") Set colDisks = objWMIService. _ ExecQuery("Select * from Win32_MappedLogicalDisk") For Each objDisk in colDisks Wscript.Echo "Device ID: " & objDisk.DeviceID Wscript.Echo "Name: " & objDisk.Name Wscript.Echo "Free Space: " & objDisk.FreeSpace Wscript.Echo "Size: " & objDisk.Size Next |
| ...defragment a hard disk? | Use the Win32_Volume class and the Defrag method.
strComputer = "." Set objWMIService = GetObject("winmgmts:\\" _ & strComputer & "\root\cimv2") Set colVolumes = objWMIService.ExecQuery _ ("Select * from Win32_Volume Where Name = 'K:\\'") For Each objVolume in colVolumes errResult = objVolume.Defrag() Next |
| ...detect which drive letter is associated with a logical disk partition? |
ComputerName = "." Set wmiServices = GetObject ( _ "winmgmts:{impersonationLevel=Impersonate}!//" _ & ComputerName) ' Get physical disk drive Set wmiDiskDrives = wmiServices.ExecQuery ( _ "SELECT Caption, DeviceID FROM Win32_DiskDrive") For Each wmiDiskDrive In wmiDiskDrives WScript.Echo "Disk drive Caption: " _ & wmiDiskDrive.Caption _ & VbNewLine & "DeviceID: " _ & " (" & wmiDiskDrive.DeviceID & ")" 'Use the disk drive device id to ' find associated partition query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" _ & wmiDiskDrive.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition" Set wmiDiskPartitions = wmiServices.ExecQuery(query) For Each wmiDiskPartition In wmiDiskPartitions 'Use partition device id to find logical disk Set wmiLogicalDisks = wmiServices.ExecQuery _ ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" _ & wmiDiskPartition.DeviceID & "'} WHERE AssocClass = Win32_LogicalDiskToPartition") For Each wmiLogicalDisk In wmiLogicalDisks WScript.Echo "Drive letter associated" _ & " with disk drive = " _ & wmiDiskDrive.Caption _ & wmiDiskDrive.DeviceID _ & VbNewLine & " Partition = " _ & wmiDiskPartition.DeviceID _ & VbNewLine & " is " _ & wmiLogicalDisk.DeviceID Next Next Next |
Related topics
Send comments about this topic to Microsoft
Build date: 11/19/2012