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.
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.
You can also provide multiple servers to get results. Just separate the servers with a comma.
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.
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.
Give it a try, let me know if this comes in useful.