{"id":518,"date":"2008-08-01T00:00:27","date_gmt":"2008-08-01T08:00:27","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/2008\/08\/01\/powershell-driveutilization\/"},"modified":"2016-10-27T13:50:23","modified_gmt":"2016-10-27T20:50:23","slug":"powershell-driveutilization","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2008\/08\/01\/powershell-driveutilization\/","title":{"rendered":"PowerShell: DriveUtilization"},"content":{"rendered":"<p>The other day <a href=\"http:\/\/www.sapien.com\/blog\/2008\/07\/30\/vbscript-drive-utilization\/\" target=\"_blank\">I posted a VBS function<\/a> for getting drive utilization. It seems only fair to give PowerShell a chance to play.<\/p>\n<div style=\"font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px;\">\n<div style=\"font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\">\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   1:<\/span> Function Get-Utilization {<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   2:<\/span>     Param([string]$computername=$env:computername,<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   3:<\/span>           [string]$ID=<span style=\"color: #006080;\">\"C:\"<\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   4:<\/span>           )<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   5:<\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   6:<\/span>      <span style=\"color: #008000;\">#suppress errors messages    <\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   7:<\/span>     $errorActionPreference=<span style=\"color: #006080;\">\"silentlycontinue\"<\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   8:<\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">   9:<\/span>     $drive=Get-WmiObject Win32_Logicaldisk -<span style=\"color: #0000ff;\">filter<\/span> <span style=\"color: #006080;\">\"DeviceID='$ID'\"<\/span> `<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  10:<\/span>     -computername $computername -errorAction <span style=\"color: #006080;\">\"silentlycontinue\"<\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  11:<\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  12:<\/span>     <span style=\"color: #0000ff;\">if<\/span> ($drive.size) {<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  13:<\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  14:<\/span>         <span style=\"color: #008000;\">#divide size and freespace by 1MB because they are UInt64<\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  15:<\/span>         <span style=\"color: #008000;\">#and I can't do anything with them in there native type<\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  16:<\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  17:<\/span>         $size=$drive.size\/1MB<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  18:<\/span>         $free=$drive.freespace\/1MB<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  19:<\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  20:<\/span>         $utilization=($size-$free)\/$size<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  21:<\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  22:<\/span>         write $utilization<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  23:<\/span>     }<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  24:<\/span>     <span style=\"color: #0000ff;\">else<\/span> {<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  25:<\/span>     <span style=\"color: #008000;\">#there as an error so return a value that can't be mistaken<\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  26:<\/span>     <span style=\"color: #008000;\">#for drive utilization<\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  27:<\/span>         write -1<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  28:<\/span>     }<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  29:<\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  30:<\/span> }<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  31:<\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  32:<\/span> <span style=\"color: #008000;\">#Sample usage<\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  33:<\/span> $computer=<span style=\"color: #006080;\">\"PUCK\"<\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  34:<\/span> $drive=<span style=\"color: #006080;\">\"C:\"<\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  35:<\/span> $u=Get-Utilization $computer $drive<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  36:<\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  37:<\/span> <span style=\"color: #008000;\">#format $u as a percentage to 2 decimal points<\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  38:<\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  39:<\/span> $percent=<span style=\"color: #006080;\">\"{0:P2}\"<\/span> -f $u<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  40:<\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  41:<\/span> <span style=\"color: #0000ff;\">if<\/span> ($u <span style=\"color: #cc6633;\">-eq<\/span> -1) {<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  42:<\/span>     $msg=<span style=\"color: #006080;\">\"WARNING: Failed to get drive {0} on {1}\"<\/span> -f $drive,$computer<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  43:<\/span>     $color=<span style=\"color: #006080;\">\"RED\"<\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  44:<\/span> }<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  45:<\/span> <span style=\"color: #0000ff;\">elseif<\/span> ($u <span style=\"color: #cc6633;\">-ge<\/span> .75) {<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  46:<\/span>     $msg=<span style=\"color: #006080;\">\"WARNING: Drive {0} on {1} is at {2} utilization.\"<\/span> -f $drive,$computer,$percent<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  47:<\/span>     $color=<span style=\"color: #006080;\">\"RED\"<\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  48:<\/span> }<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  49:<\/span> <span style=\"color: #0000ff;\">else<\/span> {<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  50:<\/span>     $msg=<span style=\"color: #006080;\">\"WARNING: Drive {0} on {1} is at {2} utilization.\"<\/span> -f $drive,$computer,$percent<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  51:<\/span>     $color=<span style=\"color: #006080;\">\"GREEN\"<\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  52:<\/span> }<\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  53:<\/span><\/pre>\n<pre style=\"font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span style=\"color: #606060;\">  54:<\/span> Write-Host $msg -foregroundcolor $color<\/pre>\n<\/div>\n<\/div>\n<p>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&#8217;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.\u00a0 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&#8217;t exist then there was some problem, perhaps permissions or an invalid drive, in this function I don&#8217;t really care. Instead I&#8217;ll simply return a value that can&#8217;t be mistaken for a drive utilization at line 27.<\/p>\n<p>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.<\/p>\n<p>You&#8217;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?<\/p>\n<div id=\"scid:fb3a1972-4489-4e52-abe7-25a00bb07fdf:e77fa90f-a341-4ce0-b9ac-1dda479c044f\" class=\"wlWriterSmartContent\" style=\"display: inline; margin: 0px; padding: 0px;\">\n<p>Download the PowerShell file <a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2008\/07\/get-utilization.txt\" target=\"_blank\">here.<\/a><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The other day I posted a VBS function for getting drive utilization. It seems only fair to give PowerShell a chance to play.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[25],"tags":[198,249,28,57,248,250,35],"class_list":["post-518","post","type-post","status-publish","format-standard","hentry","category-windows-powershell","tag-function","tag-get-wmiobject","tag-powershell","tag-scripting","tag-utilization","tag-win32_logicaldisk","tag-wmi"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/518","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=518"}],"version-history":[{"count":1,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/518\/revisions"}],"predecessor-version":[{"id":12766,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/518\/revisions\/12766"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}