Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-10-31
9
Medium Priority
?
1,154 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
ID: 39614148
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
ID: 39614188
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 59

Expert Comment

by:Bill Prew
ID: 39615748
Do you want this on all disks on the local system?

~bp
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 59

Assisted Solution

by:Bill Prew
Bill Prew earned 400 total points
ID: 39615810
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
 
LVL 65

Accepted Solution

by:
RobSampson earned 600 total points
ID: 39615860
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
ID: 39618668
Great Support friends.. you did an awesome work
0
 

Author Comment

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

Expert Comment

by:Bill Prew
ID: 39618991
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
ID: 39619175
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 Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

824 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