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.


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

  • [Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

    Technorati Tags: ,

    Tags: ,

    7 Responses to “April One-Liner”

    1. Jeffery Hicks Says:

      Big time mea culpa. This will only work with PowerShell v2.0 CTP3. I try to make sure I test on both PowerShell v1 and v2,but I obviously missed that important step. Again, my apologies.

    2. Jeffery Hicks Says:

      Here’s a WMI expression that should work in PowerShell v1 to give you essentially the same result:

      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 – $_.freespace)/1GB)}},`
      @{name=”Free(GB)”;expression={“{0:F2}” -f ($_.freespace/1GB)}},`
      @{name=”PercentFree”;expression={“{0:P2}” -f ($_.freespace/$_.size)}}

    3. Jeffery Hicks Says:

      I just can’t catch a break today. In PowerShell v1.0 the size and freespace values are of the type [int64], which doesn’t support subtraction. There’s probably an ugly hack I’ll have to find.

    4. Jeffery Hicks Says:

      This works in PowerShell v1.0. The size and freespace properties are [int64] so I have to put the numbers through some hoops.

      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)}}

      Remember all quotes should be straight.

    5. Greg Wojan Says:

      Jeff,

      I can’t get this to work properly on PowerShell v2 CTP3 either. Get-Member shows none of the properties you’re using here.

    6. Jeffery Hicks Says:

      I have a not-publicly-available later build. I have no idea where my head was when I came up with this.

    7. Greg Wojan Says:

      I figured as much. ;-) Thanks for the confirmation.


    Entries (RSS) and Comments (RSS).