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.
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.
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.