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 https://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, such as filename.vbs. 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 filename.vbs at the command prompt.
If you cannot access an event log, check to see if you are running from an Elevated command prompt. Some Event Log, such as the Security Event Log, may be protected by User Access Controls (UAC).
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 filename.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.
VB
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
Use the Win32_LogicalDisk class and check the FreeSpace property. If the value is Null, then no disk is in the drive.
VB
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
foreach ($objItem in $colItems)
{
$intFreeSpace = $objItem.FreeSpace
if ($intFreeSpace -eq $null) { "There is no disk in the floppy drive." }
else { "There is a disk in the floppy drive." }
}
...distinguish between a fixed hard disk and a removable hard disk?
Use the Win32_LogicalDisk class and check the value of the DriveType property.
VB
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 "Free Disk Space: " & objDisk.FreeSpace
Next
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