I need powershell script to find unallocated space in disk 0 across hundreds of server

    I need a powershell script  that can get me unallocated space in disk 0 from each server ( all the servers are windows 2003) as a part of audit which I have to perform across few hundreds of servers.
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

cantorisConnect With a Mentor Commented:
Here's a tweaked version of the PowerShell:

$computers = Get-Content "C:\Servers.txt"
foreach ($computer in $computers) {
    $disks = Get-WmiObject -Class Win32_DiskDrive -ComputerName $computer
    $partitions = Get-WmiObject -Class Win32_DiskPartition -ComputerName $computer
    $unpartitioned = 0
    $inUse = 0
    $partitionCount = 0
    foreach ($disk in $disks) {
        foreach ($partition in $partitions) {
            if ($partition.DiskIndex -eq $disk.Index) {
                $inUse += $partition.Size
        $obj = New-Object -TypeName PSObject
        $obj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $computer
        $obj | Add-Member -MemberType NoteProperty -Name DiskIndex -Value $disk.Index
        $obj | Add-Member -MemberType NoteProperty -Name TotalSpace -Value $disk.Size 
        $obj | Add-Member -MemberType NoteProperty -Name PartitionCount -Value $partitionCount
        $obj | Add-Member -MemberType NoteProperty -Name UnallocatedSpace -Value ((($disk.Size - $inUse) / 1GB) -as [int])
        Write-Output $obj
        $obj = $null

Open in new window

I can't fully test it as none of my drives have unallocated space.
What is your definition of unallocated space?  Do you mean physical blocks of the HDD that are not part of a file once the program (which will take hours) to run?

 If any applications are running that use files on that HDD, then this is a moving target.  NTFS allocates storage for buffer space when a file is opened in read/write mode, or has been written to.  

Or are you looking at something simple like parsing the output of DIR C:\ and looking at the free byte count that will be close, but never exact.
P.S. here is a link to source code for a sophisticated script that should meet your needs. It even bundles it up and sends the results in an email for you.


and another, less bells & whistles but easier to modify
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

If you just want free space, here's a quick example of how you could get a table of the free space in GB off the C: drive of a load of PCs in one go.  Put their hostnames in a file called ServerList.txt on your PCs C: drive for this example code.

$servers = Get-Content "C:\ServerList.txt"
$job = Get-WmiObject -ComputerName $servers -Class Win32_LogicalDisk -Filter "DeviceID='C:'" -AsJob
while ($job.State -ne "Completed") {
    Start-Sleep -Seconds 1
Receive-Job -Job $job | Select-Object @{L="Hostname";E={$_.PSComputerName}},@{L="Free Space (GB)";E={($_.FreeSpace/1GB) -as [int]}} | Format-Table -AutoSize

Open in new window

I used AsJob above so you could actually enter commands into PowerShell manually and then rather than have a while loop, just receive the job later at your convenience.

If it's truly unallocated space in a drive (ie not assigned to any partition or volume), then someone here has a method that can do that sort of thing by adding up partition sizes on a disk compared to the disk size:

globalsupportAuthor Commented:
@ Dlethe,
                Even though I like the sophisticated script that you have given it doesn't meet my needs as I need only unallocated space of a hard drive. In my organization OS is installed in Disk 0 & C:\ drive & in at least 100 servers I have unallocated space in that hard drive I have the list but I need to know the size that is left unallocated
globalsupportAuthor Commented:
@ cantoris,
                   Second script is what I also tried but the output is in KB also I don't know how to give multiple computers as input. So I tried a VBscript that was in experts exchange which meets my need, But it works for only one computer at a time if anyone can modify it to take multiple computers as input that would be sufficient. I am pasting the code below
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"
      WScript.Echo vbTab & "Unpartitioned space: " & intSpaceLeft & " MB"
The script I gave you a pretty HTML report for every computer along with Drive Label, total capacity, used capacity, free space, and free space %.  

The free space IS the unallocated space.  Change the title of the field if you want. If you are complaining about the fact that this does all HDDs instead of just the C drive, then a single IF statement will fix that.
globalsupportAuthor Commented:
            I am talking about unallocated space in the image I attached
Then you are asking about something completely different. This represents the total raw HDD capacity minus the size(s) of all defined partitions.

This script can be used as a basis to obtain what you need.

Note, if you don't have the skill set to get exactly what you want, then you could probably have somebody crank it out for you if you go to guru.com and offer $20 for a developer to give you exactly what you need as a turnkey solution.
globalsupportAuthor Commented:
             This is what i was looking for i modified the script to get the totalspace in GB & as a integer value & it works great. Thanks a ton for your effort.
I'm glad it works for you.
Are you able to readjust your points to give credit to dlethe  who found the script that I tweaked for you?
globalsupportAuthor Commented:
no I don't know how to do that
All Courses

From novice to tech pro — start learning today.