Solved

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

Posted on 2013-10-31
9
1,067 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 52

Expert Comment

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

~bp
0
 
LVL 52

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

914 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

20 Experts available now in Live!

Get 1:1 Help Now