Some of you might know that I’ve spent a lot of time on UNIX systems. One of the scripts that I used a bunch was /etc/dfspace. If you don’t know what dfspace is, it’s a simple wrapper for df that provides disk usage info in a more human readable format than the output of df. Since I really miss having that on Windows, I built it in powershell using the Get-WMIObject cmdlet. Here’s how it looks when you run it:
PS> dfspace
name Size (MB) free (MB) percent
—- ——— ——— ——-
C: 152,499.84 76,827.33 50.38
By default, it only shows me the local hard drives. By using the "-all" switch parameter I can get all the drives.
PS> dfspace -all
name Size (MB) free (MB) percent
—- ——— ——— ——-
A: 0.00 0.00 NaN
C: 152,499.84 76,826.80 50.38
D: 0.00 0.00 NaN
Z: 78,528.64 7,342.27 9.35
It can also get me the disk usage on another system via the -computer parameter (but you have to enable WMI remote access)
PS> dfspace -computer jimtrup2
name Size (MB) free (MB) percent
—- ——— ——— ——-
C: 57,231.53 11,540.28 20.16
It gives me what I like, and it’s actually a pretty simple script, where most of the script is creating the appropriate formatting
# Get-DiskUsage.ps1 (aliased to dfspace)
# Use Get-WMIObject to collect disk free info
# Can be used with remote systems
#
param ( [string]$computer = "." , [switch]$all)
# Formatting
$size = @{ l = "Size (MB)"; e = { $_.size/1mb}; f = "{0:N}"}
$free = @{ l = "free (MB)"; e = { $_.freespace/1mb}; f = "{0:N}"}
$perc = @{ l = "percent"; e = { 100.0 * ([double]$_.freespace/[double]$_.size)}; f="{0:f}" }
$name = @{ e = "name"; f = "{0,-20}" }
$fields = $name,$size,$free,$perc
# in case the user wants to see more than just local drives
$filter = "DriveType = ’3′"
if ( $all ) { $filter = "" }
# go do the work by getting the information from the appropriate
# computer, and send it to format-table with the appropriate
# fields and formatting info
get-wmiobject -class win32_logicaldisk -filter $filter -comp $computer |
format-table $fields -auto
I suppose that I could handle division by zero better, but seeing NaN doesn’t bother me. If you don’t like it, I’ll leave that as an exercise for the reader :^)