Solved

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

Posted on 2013-10-31
9
1,094 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 54

Expert Comment

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

~bp
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 54

Assisted Solution

by:Bill Prew
Bill Prew earned 100 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 150 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 54

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

730 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