PowerShell: DriveUtilization

The other day I posted a VBS function for getting drive utilization. It seems only fair to give PowerShell a chance to play.

   1: Function Get-Utilization {
   2:     Param([string]$computername=$env:computername,
   3:           [string]$ID="C:"
   4:           )
   6:      #suppress errors messages    
   7:     $errorActionPreference="silentlycontinue"
   9:     $drive=Get-WmiObject Win32_Logicaldisk -filter "DeviceID='$ID'" `
  10:     -computername $computername -errorAction "silentlycontinue"
  12:     if ($drive.size) {
  14:         #divide size and freespace by 1MB because they are UInt64
  15:         #and I can't do anything with them in there native type
  17:         $size=$drive.size/1MB
  18:         $free=$drive.freespace/1MB
  20:         $utilization=($size-$free)/$size
  22:         write $utilization
  23:     }
  24:     else {
  25:     #there as an error so return a value that can't be mistaken
  26:     #for drive utilization
  27:         write -1
  28:     }
  30: }
  32: #Sample usage
  33: $computer="PUCK"
  34: $drive="C:"
  35: $u=Get-Utilization $computer $drive
  37: #format $u as a percentage to 2 decimal points
  39: $percent="{0:P2}" -f $u
  41: if ($u -eq -1) {
  42:     $msg="WARNING: Failed to get drive {0} on {1}" -f $drive,$computer
  43:     $color="RED"
  44: }
  45: elseif ($u -ge .75) {
  46:     $msg="WARNING: Drive {0} on {1} is at {2} utilization." -f $drive,$computer,$percent
  47:     $color="RED"
  48: }
  49: else {
  50:     $msg="WARNING: Drive {0} on {1} is at {2} utilization." -f $drive,$computer,$percent
  51:     $color="GREEN"
  52: }
  54: Write-Host $msg -foregroundcolor $color

The Get-Utilization function also uses WMI via the Get-WMIObject cmdlet. One nice advantage to PowerShell is that I can specify default parameter values. I’ve set the defaults to the %computername% environmental variable and drive C:\. To make the function as seamless as possible, I turn off the error pipeline by setting $ErrorActionPreference to SilentlyContinue. I do the same with the -ErrorAction parameter in line 18.  If the Get-WMIObject expression is successful, then $drive.size will have a value and I can carry out my utilization calculations. If it doesn’t exist then there was some problem, perhaps permissions or an invalid drive, in this function I don’t really care. Instead I’ll simply return a value that can’t be mistaken for a drive utilization at line 27.

Assuming a valid drive object was returned I perform a similar calculation as I did in VBScript and write the result to the pipeline. Line 33 shows the function in use. I use an If ElseIf construct to display an appropriate message depending on the value returned by Get-Utilization. Another advantage over VBScript is that I can display the message in a different color depending on the utilization value.

You’re welcome to expand the function. For example, there is no provision for alternate credentials. You could probably add more error handling. I have to leave some fun for you right?

Download the PowerShell file here.