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
Solved

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

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

Expert Comment

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

~bp
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 53

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 53

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

808 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