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
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.
Windows 2000: Win32_DiskQuota is not available.
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
Windows 2000: Win32_VolumeChangeEvent is not available.
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
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
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
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
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
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
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
Windows 2000: Win32_MappedLogicalDisk is not available.
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
Windows 2000: Win32_Volume is not available.
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
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
Send comments about this topic to Microsoft
Build date: 11/3/2009