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

  1. 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.
  2. Open a command prompt window and navigate to the directory where you saved the file.
  3. Type cscript filename.vbs at the command prompt.
  4. 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
PowerShell
$strComputer = "."
$colItems = Get-WmiObject -Class Win32_DiskQuota -ComputerName $strComputer
foreach ($objQuota in $colItems) 
{ 
    "Volume: " + $objQuota.QuotaVolume
    "User: " + $objQuota.User      
    "Disk Space Used: " + $objQuota.DiskSpaceUsed
}
...determine when a removable drive has been added to or removed from a computer?

Use a monitoring script that queries the Win32_VolumeChangeEvent class.

VB
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.

VB
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
PowerShell
$strComputer = "."
$colItems = Get-WmiObject -Class Win32_CDROMDrive -ComputerName $strComputer
foreach ($objItem in $colItems)
{
    "Device ID: " + $objItem.DeviceID
    "MediaLoaded: " + $objItem.MediaLoaded
}
...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.

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

PowerShell
$strComputer = "."
$colItems = Get-WmiObject -Class Win32_LogicalDisk -Namespace "root\cimv2" -ComputerName $strComputer | `
    Where-Object { $_.DeviceID -eq "A:" }

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

PowerShell
$strComputer = "."
$colDisks = Get-WmiObject -Class Win32_LogicalDisk -ComputerName $strComputer 

foreach ($objDisk in $colDisks) { "DeviceID: " + $objDisk.deviceID switch ($objDisk.DriveType) { '1' { "No root directory. Drive type could not be determined." } '2' { "DriveType: Removable drive." } '3' { "DriveType: Local hard disk." } '4' { "DriveType: Network disk." } '5' { "DriveType: Compact disk." } '6' { "DriveType: RAM disk." } default: { "Drive type could not be determined." }

}

}

...determine what file system is in use on a drive?

Use the Win32_LogicalDisk class and the FileSystem 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: " & 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.

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: " & 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.

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: " & objDisk.DeviceID       
    Wscript.Echo "Disk Size: " & objDisk.Size
Next
...find out what drives are mapped on a computer?

Use the Win32_MappedLogicalDisk class.

VB
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.

VB
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?
  1. Start with the Win32_DiskDrive class and query for instances of Win32_DiskPartition using the DeviceID property and the Win32_DiskDriveToDiskPartition association class. Now you have a collection of the partitions on the physical drive.
  2. Query for the Win32_LogicalDisk that represents the partition using the Win32_DiskPartition.DeviceID property and Win32_LogicalDiskToPartition association class.
  3. Get the drive letter from the Win32_LogicalDisk.DeviceID.

VB
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

WMI Tasks for Scripts and Applications

WMI C++ Application Examples

TechNet ScriptCenter

`