WMI can provide information about the local and mapped drives on a computer. This includes virtual drives and CD-ROM drives.

The following code sample will query the Win32_LogicalDisk class and return various properties about each hard drive, removable disk, floppy drive and so on.

If the drive is a local or network hard drive, it will also display the file system (i.e. FAT32, NTFS), and the UNC path for network drives.

Sub TestDriveSpace()
' http://msdn.microsoft.com/en-us/library/aa394592(VS.85).aspx

Dim objWMI As Object
Dim colDisks As Object
Dim objDisk As Object
Dim strDriveType As String

  Set objWMI = GetWMIService

  ' determine free space
  Set colDisks = objWMI.ExecQuery _
                 ("Select * from Win32_LogicalDisk")
  For Each objDisk In colDisks

    With objDisk

      Debug.Print "DeviceID: " & .DeviceID
      ' assume GB
      Debug.Print "Size: " & Format(.Size / 1000000000, "# GB")
      ' assume GB
      Debug.Print "Free Disk Space: " _
                & Format(.FreeSpace / 1000000000, "# GB")
      Debug.Print "% Free: " & Format(.FreeSpace / .Size, "Percent")

      ' http://msdn.microsoft.com/en-us/library/aa394173(VS.85).aspx
      Select Case .DriveType
        Case 0
          strDriveType = "Unknown"
        Case 1
          strDriveType = "No Root Directory"
        Case 2
          strDriveType = "Removable Disk"
        Case 3
          strDriveType = "Local Disk"
        Case 4
          strDriveType = "Network Drive"
        Case 5
          strDriveType = "Compact Disc"
        Case 6
          strDriveType = "RAM Disk"
      End Select

      Debug.Print "Drive Type: " & strDriveType
      If (strDriveType = "Local Disk") Or (strDriveType = "Network Drive") Then
        Debug.Print "File System: " & .FileSystem
      End If
      If strDriveType = "Network Drive" Then
        Debug.Print "Provider Name: " & .ProviderName
      End If
      Debug.Print "---"

    End With

  Next objDisk

End Sub

Function GetWMIService() As Object
' http://msdn.microsoft.com/en-us/library/aa394586(VS.85).aspx
Dim strComputer As String

  strComputer = "."

  Set GetWMIService = GetObject("winmgmts:" _
                              & "{impersonationLevel=impersonate}!\\" _
                              & strComputer & "\root\cimv2")
End Function

Mapped Drives Information

Remote drives that are locally mapped can be accessed as above, but they also get their own class, Win32_MappedLogicalDisk.

The code is much the same as above, except only mapped drives are shown.

Sub TestMappedDrives()

Dim objWMI As Object
Dim colDisks As Object
Dim objDisk As Object

  Set objWMI = GetWMIService

  Set colDisks = objWMI. _
                 ExecQuery("Select * from Win32_MappedLogicalDisk")
  For Each objDisk In colDisks
    Debug.Print "Device ID: " & objDisk.DeviceID
    Debug.Print "Name: " & objDisk.Name
    Debug.Print "Free Space: " & objDisk.FreeSpace
    Debug.Print "Size: " & objDisk.Size
    Debug.Print "---"
  Next

End Sub

Site last updated: February 12, 2012