Quick Powershell Disk Space Check

Today I received a request to copy some database backups over to a file share. As a part of performing this process I generally log on to the target first to check that there is sufficient free space to complete the copy. As I was waiting for the server login to complete I knew that there really had to be a better way to do this.

There are lots of tools out there that can get this data however I realized that Powershell could do just this quickly and easily. In less than an hour I had a working function deployed to my profile that I could quickly and easily call to get the information on all disks on a machine.


   1: Function DiskSpace ($Servername)

   2: {

   3: GWMI -ComputerName $Servername Win32_LogicalDisk -Filter "DriveType = 3 AND Size > 0" | 

   4:     Select SystemName, 

   5:     @{Label="Drive";Expression={$_.DeviceID}}, 

   6:     VolumeName, 

   7:     @{Label="Size GB";Expression={"{0:N1}" -f($_.size/1GB)}}, 

   8:     @{Label="Free GB";Expression={"{0:N1}" -f($_.freespace/1GB)}} , 

   9:     @{Label="Free Percent";Expression={"{0:N1}" -f(($_.freespace / $_.size) * 100)}} | 

  10:     Out-GridView

  11: }


Once added to your profile (for more about profiles check out http://msdn.microsoft.com/en-us/library/bb613488(VS.85).aspx ) it’s a simple case of opening powershell and calling the function along with the machine name.

DiskSpace Server1

You can also provide multiple servers to get results. Just separate the servers with a comma.

DiskSpace Server1,Server2,Server3



Shahryar Hashemi (Twitter) pointed out quite correctly on Twitter that this wouldn’t cover mount points (as I only have them on a single machine I hadn’t thought to cover that). I’ve updated the script to use Win32_Volume which gives us information on the mount points. This basic script now won’t give you the volume names however it will give you the path to the mount point. 

Code Snippet
  1. Function DiskSpace ($Servername)
  2. {
  4. GWMI -ComputerName $Servername Win32_Volume -Filter “DriveType = 3 AND Capacity > 0” |
  5.      Select SystemName,
  6.     @{Label=“Drive”;Expression={$_.Caption}},
  7.      ##VolumeName,
  8.     @{Label=“Size GB”;Expression={“{0:N1}” -f($_.Capacity/1GB)}},
  9.     @{Label=“Free GB”;Expression={“{0:N1}” -f($_.freespace/1GB)}} ,
  10.     @{Label=“Free Percent”;Expression={“{0:N1}” -f(($_.freespace / $_.Capacity) * 100)}} |
  11.     Out-GridView
  15. }

Also, check out what Dave Levy (blog|twitter) has done with this http://adventuresinsql.com/2010/11/get-drive-space-including-mount-points/



Based on the great suggestion by Dave Levy on being able to provide a unit of measure for the function (so it can return capacity and freespace in something other than just GB) here’s another updated version of the script. This time pass in the server name and the unit of measure you want to use (KB/MB/GB). If you don’t pass in a value it will give you the space information in GB. Just a note, if you pass in something invalid you won’t get space information at all. Like I said, quick and dirty, there’s no error handling going on here folks.

Code Snippet
  1. Function DiskSpace2 ($Servername, $unit)
  2. {
  4. IF (!$unit) {$unit = “GB”}
  5. $measure = “1$unit”
  7. GWMI -ComputerName $Servername Win32_Volume -Filter “DriveType = 3 AND Capacity > 0” |
  8.     Select SystemName,
  9.     @{Label=“Drive”;Expression={$_.Caption}},
  10.     ##VolumeName,
  11.     @{Label=“Size $unit”;Expression={“{0:N1}” -f($_.Capacity/$measure)}},
  12.     @{Label=“Free $unit”;Expression={“{0:N1}” -f($_.freespace/$measure)}} ,
  13.     @{Label=“Free Percent”;Expression={“{0:N1}” -f(($_.freespace / $_.Capacity) * 100)}} |
  14.     Out-GridView
  16. }


Give it a try, let me know if this comes in useful.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s