Free Space on Disks

by Jan 2, 2009

You can use WMI to determine how much free space is available on any given disk:

Get-WMIObject Win32_LogicalDisk | Foreach-Object { 
'Disk {0} has {1:0.0} MB space available' -f $_.Caption, ($_.FreeSpace / 1MB) }

Here is how it works:

Get-WMIObject returns instances of a WMI class. The WMI class Win32_LogicalDisk represents disk drives. Each drive is then processed within the foreach-object script block and represented by the special variable $_.

Using the -f formatting operator, it is easy to fill in the requested information by reading the Caption and FreeSpace property. Note that you can convert the bytes to megabytes by dividing bytes by 1MB. If you wanted gigabytes, change 1MB to 1GB. Just make sure you don't have a space between number and unit. 1 MB will not work.