Solved

How to get the unallocated space of a Disk using vbscript?

Posted on 2013-10-31
9
1,056 Views
Last Modified: 2013-11-02
Hi Guys,

Can anybody help me to create a script which will get the value of unallocated free space of a disk.
0
Comment
Question by:kbjijo
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 7

Expert Comment

by:Ironhoofs
Comment Utility
You can get this information from the FileSystemObject.

Set fso = CreateObject("Scripting.FileSystemObject")
Set drive = fso.GetDrive("C:")
MsgBox(drive.FreeSpace)

Open in new window

0
 

Author Comment

by:kbjijo
Comment Utility
this is giving the free space of C or any other drive.. but my requirment is to get unallocated space size from the disk. (in the below scenario 20Gb, result from diskpart)

  Disk ###  Status      Size         Free     Dyn  Gpt
  --------  ----------  -------  -------     ---  -       --
* Disk 3    Online       273 GB    20 GB
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Do you want this on all disks on the local system?

~bp
0
 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 100 total points
Comment Utility
Just to get a sense of a starting place, try this VBS script on the system you want the info for, and post up the output.

ComputerName = "."
Set wmiServices = GetObject ("winmgmts:{impersonationLevel=Impersonate}!//" & ComputerName & "\root\cimv2")
Set wmiDiskDrives = wmiServices.ExecQuery ("SELECT DeviceID, Size, MediaType FROM Win32_DiskDrive")

For Each wmiDiskDrive In wmiDiskDrives
    query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" & wmiDiskDrive.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"    
    Set wmiDiskPartitions = wmiServices.ExecQuery(query)

    For Each wmiDiskPartition In wmiDiskPartitions
        Set wmiLogicalDisks = wmiServices.ExecQuery ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" & wmiDiskPartition.DeviceID & "'} WHERE AssocClass = Win32_LogicalDiskToPartition") 
        For Each wmiLogicalDisk In wmiLogicalDisks
        		strWindowsDrive = wmiLogicalDisk.DeviceID
                Wscript.Echo wmiDiskDrive.DeviceID & "," & wmiDiskDrive.Size & "," & strWindowsDrive & "," & wmiDiskPartition.Size & "," & (wmiDiskDrive.Size - wmiDiskPartition.Size)
        Next      
    Next
Next

Open in new window

~bp
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 65

Accepted Solution

by:
RobSampson earned 150 total points
Comment Utility
Hi, give this script a go.  Run it from a command prompt with
cscript C:\Scripts\UnallocatedSpace.vbs

Regards,

Rob.

strComputer = "."
Set wmiServices = GetObject("winmgmts:{impersonationLevel=Impersonate}!//" & strComputer)
Set wmiDiskDrives = wmiServices.ExecQuery("SELECT Caption, DeviceID, Size FROM Win32_DiskDrive")
For Each wmiDiskDrive In wmiDiskDrives
	intDiskSpace = Round((wmiDiskDrive.size / 1024 / 1024), 2)
	WScript.Echo wmiDiskDrive.Caption & " (" & wmiDiskDrive.DeviceID & ") " & intDiskSpace & " MB"
	intSpaceLeft = intDiskSpace
	strEscapedDeviceID = Replace(wmiDiskDrive.DeviceID, "\", "\\", 1, -1, vbTextCompare)
	Set wmiDiskPartitions = wmiServices.ExecQuery("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & strEscapedDeviceID & """} WHERE AssocClass = Win32_DiskDriveToDiskPartition")
	For Each wmiDiskPartition In wmiDiskPartitions
		intPartitionSpace = Round((wmiDiskPartition.Size / 1024 / 1024), 2)
		WScript.Echo vbTab & "Partition ID: " & wmiDiskPartition.DeviceID & ", " & intPartitionSpace & " MB"
		intSpaceLeft = Round(intSpaceLeft - intPartitionSpace, 2)
		Set wmiLogicalDisks = wmiServices.ExecQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & wmiDiskPartition.DeviceID & """} WHERE AssocClass = Win32_LogicalDiskToPartition")
		For Each wmiLogicalDisk In wmiLogicalDisks
			WScript.Echo vbTab & vbTab & "Logical Disk " & wmiLogicalDisk.DeviceID & ", " & (wmiLogicalDisk.Size / 1024 / 1024) & " MB"
		Next
	Next
	WScript.Echo vbTab & "Unpartitioned space: " & intSpaceLeft & " MB"
Next

Open in new window

0
 

Author Comment

by:kbjijo
Comment Utility
Great Support friends.. you did an awesome work
0
 

Author Comment

by:kbjijo
Comment Utility
But in some places i am getting a minus (-) value as unpartitioned space.. how it possible ?!!
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Yes, I saw that on one of the disks on my system.  I suspect it's due to some rounding or inaccuracy in the calculations.  I would suggest that you treat negatives as zero.

~bp
0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
I had that too, by about 600 kb, which isn't a concern. Above the WScript.Echo line that tell you the unpartitioned space, add
If intSpaceLeft < 0 Then intSpaceLeft = 0

Open in new window


Rob
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Welcome to part one of a multi-part tutorial series, VBScript for Windows System Administrators.  The goal of this series is to teach non-programmers how to write useful VBS code to automate their environment, and perform tasks faster, and in a more…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now