Use PowerShell to Create a Report Displaying Free Disk Space

Doctor Scripto

Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to create a report that displays free disk space on servers.

Hey, Scripting Guy! Question Hey, Scripting Guy! I know it may be hard to believe in today’s era of multiterabyte disks on laptops, but our servers are constantly running out of disk space. Part of the problem is that they were built several years ago, and with all of the patches, service packs, software installed and uninstalled, registry bloat, and whatever else, we are constantly running out of disk space. We are a small shop, and we do not have a network monitoring program such as System Center. What we do have is a CIO who has no clues about how computers work, and who goes herbitile when any server is down for even five seconds, not to mention when a server is down for a couple of days and we have to order a new drive and restore things from tape to rebuild the system drive. I need something that will produce a daily report letting me know the amount of free disk space on each of the drives. Can you help me out? I have seen some scripts on the Script Repository, but nothing that creates a very nice report.


Hey, Scripting Guy! Answer Hello DV,

Microsoft Scripting Guy, Ed Wilson, is here. Today it is cool down here in Charlotte, North Carolina in the southern portion of the United States. It is because the skies are cloudy and it is threatening thunder storms. But hey, it is cooler than it has been in a week, so we will accept any temperature relief offered.

DV, because it is a nice cool day down here, I am going to cheat a bit and use the report and the same basic script I wrote yesterday. What I will do is add free disk space to the report—that should work for you.

First find the information

Note   This is the second blog in a series that discusses creating an HTML report that contains server status information. You should read yesterday’s blog, Use PowerShell to Create an HTML Uptime Report, prior to reading today’s.

The WMI class Win32_Volume (in the operating system since Windows Server 2003) contains a property named freespace. Unfortunately, this property displays in bytes, which means that the numbers are rather large and difficult to comprehend. A generic query to Win32_Volume returns the CD-ROM drive, mapped network drives, and other things you may not be interested in receiving. However, a drive type of 3 is a fixed local disk, and it is the information you want to receive. To fix the output requires a couple of things. First, it requires converting the free space from bytes to gigabytes. To do this, I divide by the GB admin constant. I add it to a custom object, and I create a custom label. This technique is shown here.

PS C:\> gwmi win32_volume -Filter ‘drivetype = 3’ | select driveletter, label, @{LABE

L=’GBfreespace’;EXPRESSION={$_.freespace/1GB} }


driveletter                  label                                       GBfreespace

———–                  —–                                       ———–

                             System Reserved                       0.106239318847656

C:                                                                  60.1407623291016

Unfortunately, the output is still a bit difficult to read due to the great amount of precision following the decimal. I now use a format specifier to change the way the numbers display. This is shown here.

PS C:\> gwmi win32_volume -Filter ‘drivetype = 3’ |

select driveletter, label, @{LABEL=’GBfreespace’;EXPRESSION={“{0:N2}” -f ($_.freespace/1GB)} }


driveletter                  label                       GBfreespace

———–                  —–                       ———–

                             System Reserved             0.11

C:                                                       60.14

Break up the report

Unfortunately, I cannot just add the disk information as an additional property on the custom object and have it unravel onto the report. This is because all the servers have multiple drives, and I therefore end up with an array of disk objects that I must report. The solution is to break-up the report a bit, and report the information in fragments. Therefore, I go back to my code from yesterday, and modify it just a little so that instead of writing directly to the report, I create a fragment and store the returned fragment in a variable. Also note that you cannot use the fragment and the body parameters together. I decide to change the report title from <h1> to <h2> because I am going to use a different report heading. <h2> as you might imagine is a bit smaller than <h1>.

$upTime = Get-UpTime -servers $servers |

ConvertTo-Html -As Table -Fragment -PreContent “

  <h2>Server Uptime Report</h2>

  The following report was run on $(get-date)” | Out-String

Add the new function and call the function

I now create a function to gather the disk space information. The function contains the essential code that I created at the Windows PowerShell console, but wrapped as a function. 

Function Get-DiskSpace


 Param ([string[]]$servers)

  Foreach ($s in $servers)


     Get-WmiObject -Class win32_volume -cn $s |

       Select-Object @{LABEL=’Comptuer’;EXPRESSION={$s}},

         driveletter, label,

         @{LABEL=’GBfreespace’;EXPRESSION={“{0:N2}” -f ($_.freespace/1GB)}}

    } #end foreach $s

} #end function Get-DiskSpace

I copy the same essential code that I used to create the Uptime fragment to create the disk space fragment. I use the <h2> tag as a table header to let me know which section of the report appears.

$disk = Get-DiskSpace -servers $servers |

ConvertTo-Html -As Table -Fragment -PreContent “

  <h2>Disk Report</h2> “| Out-String   

Create the new report

Now I need to create the report. First, I add precontent. This content appears at the top of the report. I use the same type of <h1> tag that I used yesterday for the report title. Now the post content consists of the two HTML fragments that I created via the two functions. Instead of converting the entire thing to a string, I write it out to the file, and then I use the Invoke-Item cmdlet to launch the report. This code is shown here.

$disk = Get-DiskSpace -servers $servers |

ConvertTo-Html -As Table -Fragment -PreContent “

  <h2>Disk Report</h2> “| Out-String 


ConvertTo-Html -PreContent “<h1>Server Uptime and Disk Report</h1>” `

  -PostContent $upTime, $disk >> $path

 Invoke-Item $path 

The completed report appears in the following image.

Image of command output

I uploaded the complete HTML_UptimeFreeSpaceReport.ps1 script to the Scripting Guys Script Repository.

DV, that is all there is to using Windows PowerShell to create an HTML free disk space report. Join me tomorrow for more cool Windows PowerShell stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

1 comment

Discussion is closed. Login to edit/delete existing comments.

  • Alan Gunn 0

    Hi! A google search reveals that this is the only page on the entire interwebs to contain the words “goes herbitile”.
    I believe this makes it a Googlewhack! 🙂

Feedback usabilla icon