Getting Disk Usage Information   4 comments

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 :^)


Posted September 8, 2006 by jtruher3 in PowerShell

4 responses to “Getting Disk Usage Information

Subscribe to comments with RSS.

  1. I\’m new to PS, I\’ve been looking all over to do disk usage in Windows in general and PS.  With no parameters it works.
    I\’m attempting to get the disk usage of a network path (\\\\velocity-fs01\\DSAN11)PS C:\\apps\\powershellide> C:\\apps\\powershellide\\Get-DiskUsage.ps1 \\\\velocity-fs01\\DSAN11
    But I\’m getting an error:Get-WmiObject : Invalid parameterAt C:\\apps\\powershellide\\Get-DiskUsage.ps1:20 char:14+ get-wmiobject  <<<< -class win32_logicaldisk -filter $filter -comp $computer|
    Also I tried something simple, to get the disk usage of the temp directory and no luck:PS C:\\apps\\powershellide> C:\\apps\\powershellide\\Get-DiskUsage.ps1 c:\\tempGet-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)At C:\\apps\\powershellide\\Get-DiskUsage.ps1:20 char:14+ get-wmiobject  <<<< -class win32_logicaldisk -filter $filter -comp $computer|I\’m not sure how to fix it.  Any thoughts?Randy

  2. Hi,
    When I tried to use your script I received the following error:
    Bad numeric constant: 1m.At C:\\Documents and Settings\\kristofd\\My Documents\\Powershell scripts\\Get-DiskUsage.ps1:7 char:44+ $size = @{ l = "Size (MB)"; e = { $_.size/1m <<<< };      f = "{0:N}"}
    I changed the value 1m into \’1048576\’ and then it seemed to work.
    Can you give me a bit more info about the value 1m you used and what it\’s supposed to do?
    I\’m getting this error on a Win XP SP2.
    Kristof Degraen

  3. This was created so long ago, the m postfix designation was in effect.  It was changed to mb shortly before release.  I\’ve updated the code in the example – sorry for the confusion!

  4. This is terrific! Thank you!You really helped me out!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: