WMI: VBscript to get the freespace of all drives of a computer

Posted February 1st, 2010 in disk, wmi by dirk adamsky

This is another beginner script.
Although it is not very long it is very powerful.
It can be used for monitoring (I have used a slightly modified version in on a cacti (windows based) monitoring server).
Default computer is the local machine.
When the dot is replaced by another machine name the script works against that machine.
There are some conditions that must be met:

  1. the script must be run with administrative credentials
  2. the firewall of the remote machine needs the following ports opened for WMI: TCP 135, TCP 4168 and
    UDP 9256

Follow the next steps to make and run the script (admin rights needed):

  • open your favorite text editor (mine is notepad++)
  • copy and paste the script into the editor (delete the line numbers)
  • save the script (for example c:tempfreespace.vbs)
  • open a command prompt
  • go to “c:temp”
  • give “cscript freespace.vbs” (without quotes) and enter

The script:

' Name : freespace.vbs
' Description : script to get the freespace of all drives of a computer
' Author : dirk adamsky - deludi bv
' Version : 1.00
' Date : 01-02-2010
' Level : beginner

strComputer = "." ' create a variable for the computer name
Set objWMI = GetObject("winmgmts:\" & strComputer & "rootcimv2") ' create the WMI object
Set colItems = objWMI.ExecQuery("Select * from Win32_LogicalDisk") ' create a logicaldisk collection object
For Each Item in colItems ' for each drive in the collection
	If Item.Size <> "" Then ' if the size is not empty echo drive and rounded freespace in MB
		Wscript.Echo "Drive: " & Item.Name & " has " & Round(Item.Freespace/1048576) & " MB free space"
	End If
Next
Set colItems = Nothing ' close objects
Set objWMIService = Nothing

As said earlier: when you have questions/problems please give a reply.

Happy scripting.

Best regards,

Dirk Adamsky – Deludi BV