3

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

Posted February 1st, 2010 in disk, wmi and tagged , , , , , , 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


Related Posts:
  • VBscript and WMI: VBscript to enumerate all mailboxes on a given Exchange server
  • Active Directory: VBscript to enumerate the message restrictions (send to rights) of a distributionlist
  • VBscript: vbscript to enumerate the printers of Active Directory users and write the results to a file

  • 3 Responses so far.

    1. SHILPA says:

      This code is really wonderful. Thanks for help.

    2. daniel says:

      i would like the script to get the free diskspace of all the drives on a remote computers e.g 14 drives , and return with the driveletter that has most free space available e.g F
      i can’t figure out how to do this can you point me in a right direction

      thx
      for the script so far

    3. dirk adamsky says:

      Hi Daniel,

      Here’s an extended version that gives a sorted list as output:

      strComputer = "."
      Set objDisconnectedRecordset = CreateObject("ADODB.Recordset")
      objDisconnectedRecordset.Fields.Append "Drive", 200, 255
      objDisconnectedRecordset.Fields.Append "Size", 5
      objDisconnectedRecordset.Open
      Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
      Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk")
      
      For Each Item in colItems
      	If Item.Size <> "" Then
      		objDisconnectedRecordset.AddNew
      		objDisconnectedRecordset("Drive").Value = Item.Name
      		objDisconnectedRecordset("Size").Value = Round(Item.Size/1048576)
      		objDisconnectedRecordset.Update
      	End If
      Next
      Set colItems = Nothing
      Set objWMIService = Nothing
      
      objDisconnectedRecordset.Sort = "Size DESC" ' Use DESC/ASC to specify sort order.
      objDisconnectedRecordset.MoveFirst
      
      Do Until objDisconnectedRecordset.EOF
          WScript.Echo objDisconnectedRecordset.Fields.Item("Drive") _
              & " ; " & objDisconnectedRecordset.Fields.Item("Size") & " MB"
          objDisconnectedRecordset.MoveNext
      Loop 
      
      Set objDisconnectedRecordset = Nothing
      

      Hope this is what you need.

      Best regards,

      dirk adamsky

    Leave a Reply