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
UPDATE:
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.
- Function DiskSpace ($Servername)
- {
- GWMI -ComputerName $Servername Win32_Volume -Filter “DriveType = 3 AND Capacity > 0” |
- Select SystemName,
- @{Label=“Drive”;Expression={$_.Caption}},
- ##VolumeName,
- @{Label=“Size GB”;Expression={“{0:N1}” -f($_.Capacity/1GB)}},
- @{Label=“Free GB”;Expression={“{0:N1}” -f($_.freespace/1GB)}} ,
- @{Label=“Free Percent”;Expression={“{0:N1}” -f(($_.freespace / $_.Capacity) * 100)}} |
- Out-GridView
- }
Also, check out what Dave Levy (blog|twitter) has done with this http://adventuresinsql.com/2010/11/get-drive-space-including-mount-points/
UPDATE 2:
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.
- Function DiskSpace2 ($Servername, $unit)
- {
- IF (!$unit) {$unit = “GB”}
- $measure = “1$unit”
- GWMI -ComputerName $Servername Win32_Volume -Filter “DriveType = 3 AND Capacity > 0” |
- Select SystemName,
- @{Label=“Drive”;Expression={$_.Caption}},
- ##VolumeName,
- @{Label=“Size $unit”;Expression={“{0:N1}” -f($_.Capacity/$measure)}},
- @{Label=“Free $unit”;Expression={“{0:N1}” -f($_.freespace/$measure)}} ,
- @{Label=“Free Percent”;Expression={“{0:N1}” -f(($_.freespace / $_.Capacity) * 100)}} |
- Out-GridView
- }
Give it a try, let me know if this comes in useful.