April One-Liner

Here’s another way to get disk utilization using your PSDrives. This is a one line command.  I’ve broken it up with the line continuation character. This only works locally, only queries PSDrives using the FileSystem provider and that
have a value for the Used property.

Get-PSDrive -PSProvider filesystem | where {$_.used} |
select Root,@{name="Size(GB)";expression={($_.used+$_.free)/1GB -as [int]}},`
@{name="Used(GB)";expression={"{0:F2}" -f ($_.used/1GB)}},`
@{name="Free(GB)";expression={"{0:F2}" -f ($_.free/1GB)}},`
@{name="PercentFree";expression={"{0:P2}" -f ($_.free/($_.used+$_.free))}}

The only thing I’ve done is customize the output to show utilization sizes in GB and calculate a PercentFree value. The expression returns information like this:

Root        : C:\
Size(GB)    : 75
Used(GB)    : 66.80
Free(GB)    : 7.73
PercentFree : 10.37 %

Root        : F:\
Size(GB)    : 75
Used(GB)    : 7.53
Free(GB)    : 67.00
PercentFree : 89.90 %

You could also pipe this expression to Format-Table to make a nicer looking report.

Download the oneliner here.

UPDATE: Since I originally posted this I realized I made a grievous error. This oneliner likely won’t work for you, and definitely not on PowerShell v1.0. I normally test on all versions but missed something this month. My apologies. Here is a comparable expression that uses WMI to achieve the same results.

get-wmiobject win32_logicaldisk -filter "size > 0" |
select DeviceID,@{name="Size(GB)";expression={($_.size)/1GB -as [int]}},`
@{name="Used(GB)";expression={
"{0:F2}" -f ((((($_.size/1mb) - ($_.freespace/1mb))*1mb)/1gb))}},`
@{name="Free(GB)";expression={"{0:F2}" -f ($_.freespace/1GB)}},`
@{name="PercentFree";expression={"{0:P2}" -f ($_.freespace/$_.size)}}