Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Drive Report Console Chart

Posted on October 15, 2009

In thinking about some of my recent posts, I realize I should make clear that these scripts and functions are not necessarily good PowerShell examples. They don’t take advantage of objects and the pipeline. They are single purpose and one-dimensional. Not that there’s anything wrong with that. My recent examples, and the one I have today, are experiments in exploiting the console and host itself. The script I worked up for today presents a colored bar “graph” showing disk utilization directly in the console.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

The script uses Get-WMIObject to query all fixed local disks. It takes the size and free space properties and calculates some usage information. Normally I’d simply write the object to the pipeline, but I decided to take advantage of Write-Host and create a bar graph for each drive.

drivereport screenshot

The value to the left is the free size in GB. The percentage to the right reflects utilization.  The script takes the percentage free value and writes out that many spaces. By using the –BackgroundColor parameter, I can create a colored bar chart. I use different colors to represent the decline in free space.

Here’s the script:

Param ([string]$computername=$env:computername,

        [Management.Automation.PSCredential]$credential)

        

Trap {

#if an exception occurs, display a warning message and bail

    Write-Warning "There was a problem getting WMI information from $computername."

    Write-Warning $_.Exception.Message

    Return

}

 

if ($credential) {

 

    $drives=Get-WmiObject win32_logicaldisk -Filter "drivetype=3" -ComputerName $computername -Credential $credential -ea Stop

}

 

else {

    $drives=Get-WmiObject win32_logicaldisk -Filter "drivetype=3" -ComputerName $computername -ea Stop

}

 

Write-Host $computername.ToUpper()

 

foreach ($drive in $drives) {

 

    [int64]$size=$drive.size

    [int64]$free=$drive.Freespace

    [int]$bar=($free/$size) *100

    

    $used=($size-$free)/$size

    

    if ($bar -ge 80) {

        $color="Green"

    }

    elseif ($bar -ge 60) {

        $color="DarkGreen"

    }

    

    elseif ($bar -ge 40) {

        $color="Yellow"

    }

    

    elseif ($bar -ge 20 ) {

        $color="Magenta"

    }

    else {

        $color="Red"

    }

    

    $data="{0} {1:N2}GB free {2} {3:P2}" -f $drive.DeviceiD,($free/1GB),(" " * $bar),$used

    Write-Host $data -BackgroundColor $color -ForegroundColor "black"

   

} #end ForEach

This is a script, although you could write it as a function. The script takes a computer name as a parameter, defaulting to the local computer. It also takes a PSCredential but you have to pass it an credential object previously created. If you look at the beginning of the screen shot you can see an example of how to use the script. The $mycompany object is a PSCredential I created earlier. This script should work in PowerShell v1.0 and 2.0.

Again, there’s not much you can do with the script because it writes nothing to the pipeline which I think defeats the purpose of PowerShell. On the other hand, I think a script like this demonstrates capabilities that are beyond the CMD shell, unless you go back to the days of ANSI.

So for newcomers to PowerShell, look at these types of scripts as exceptions or special use cases, and not examples of effective PowerShell.


Behind the PowerShell Pipeline
Download DriveReport.ps1

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d